哪有什么岁月静好,不过是有人替你负重前行!

项目添加vue-router

首先安装:

npm i vue-router -S

在main.js中导入:

import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

推荐使⽤:vue add router 添加插件(记得提前提交)

基本使⽤

router.js

import Vue from 'vue'
//1.导⼊
import Router from 'vue-router'
import Home from './views/Home.vue'
import About from './views/About.vue'
//2.模块化机制 使⽤Router
Vue.use(Router)
//3.创建路由器对象
const router = new Router({
 routes:[{
 path: '/home',
 component: Home
 },
 {
 path: '/about',
 component: About
 }
 ]
})
export default router;

main.js

import Vue from 'vue'
import App from './App.vue'
import router from './router'
Vue.config.productionTip = false
new Vue({
 // 4.挂载根实例
 router,
 render: h => h(App)
}).$mount('#app')

做好以上配置之后

App.vue

<template>
 <div id="app">
 <div id="nav">
 <!-- 使⽤router-link组件来导航 -->
 <!-- 通过传⼊to属性指定连接 -->
 <!-- router-link默认会被渲染成⼀个a标签 -->
 <router-link to="/">Home</router-link> |
 <router-link to="/about">About</router-link> |
 </div>
 <!-- 路由出⼝ -->
 <!-- 路由匹配的组件将被渲染到这⾥ -->
 <router-view/>
 </div>
</template>

  打开浏览器.,切换Home和About超链接,查看效果

posted @ 2022-03-01 19:03  longfei825  阅读(31)  评论(0编辑  收藏  举报