Using routing, one exit can jump to two pages.
Effect
implementation steps:
Step 0, install routing
When installing routing, pay attention to your vue version. If you run npm install vue-router
the command directly, the latest version will be downloaded. The latest version is suitable for vue3.0. I am using version 2.7, so execute the npm install --save vue-router@3
command.
If you need to uninstall the previous route, you can do so npm uninstall vue-router
.
The following is directly written in main.js :
import Vue from 'vue'
import App from './App.vue'
Vue.config.productionTip = false
//1. 安装 引入路由
import VueRouter from 'vue-router'
Vue.use(VueRouter)
//2. 定义路由组件
import Home from "./views/Home.vue"
import news from "./views/news.vue"
//3.定义路由- 配置路由走向
const routes = [
{
path: "/", //页面的路径
component: Home //去哪个页面
},
{
path: "/news", //页面的路径
component: news //去哪个页面
}
]
//4. 创建router实例,然后传“routes”配置
const router = new VueRouter({
//routes : routes
routes
})
//5. 创建和挂载根实例
new Vue({
router, //router: router
render: h => h(App),
}).$mount('#app')
Before this, I created two pages. How to display them without pages:
Note that the pages here are in the src directory. The name can be defined by yourself, usually called 'view'.
Finally, it can be displayed directly in App.vue. .
<template>
<div id="app">
app
<!-- 显示导航 -->
<div>
<router-link to="/">首页</router-link> |
<router-link to="/news">新闻页✌</router-link>
</div>
<!-- 显示路由出口 -->
<router-view></router-view>
</div>
</template>
<script>
export default {
name: 'App',
components: {
}
}
</script>
<style lang="less">
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>