vue组件实现简单的路由

首先在当前项目下安装依赖包,运行npm install  vue-router  则package.json中添加了vue-router

此时在src 下的index.js主程序中引入vue-router,同时告诉Vue使用此路由配置

1 import VueRouter from "vue-router"
2 Vue.use(VueRouter);

index.js里面引入需要配置的组件的信息

1 import Vmain from "@/components/Vmain.vue"
2 import Vcourse from "@/components/Vcourse.vue"
3 import Vmarke from "@/components/Vmarke.vue"

实例化一个路由对象,并把配置路由列表传进去,路由可以去除默认的井号配置 mode:"history"

1 const router=new VueRouter({
2   mode:"history",
3   routes:[
4     {path:"/",component:Vmain},
5     {path:"/Vcourse",component:Vcourse},
6     {path:"/Vmarke",component:Vmarke},
7     ]
8 });

把当前路由对象挂载到Vue对象中去,在键值相同的情况下,可以省略用,代替

1 new Vue({
2   el: '#app',
3   router,
4   components: { App },
5   template: '<App/>'
6 });

在父组件app.vue里面,如果想用子组件路由,则必须加入 <router-view></router-view>路由出口

然后可以用<router-link to=" ">xxx</router-link> 去替代<a href="">xxx</a>

默认会加载出 "/"对应的路由

 

转载于:https://www.cnblogs.com/wen-kang/p/9979918.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值