npm install vue-router -s
npm install
src下新建views文件夹,新建Home.vue、Products.vue
<template>
<div>home </div>
</template>
<script>
export default {
}
</script>
<style>
</style>
src下新建routes.js
import Home from './views/Home'
import Products from './views/Products'
export const routes = [
{ path: '/home', component: Home },
{ path: '/products/:id', component: Products }
]
在main.js引入
import Vue from 'vue'
import App from './App.vue'
import VueRouter from 'vue-router' //1. 引入路由模块
import {routes} from './routes' //2.引入静态路由表
Vue.use(VueRouter); //3.使用路由模块
//4.创建一个VueRouter模块的实例
const router = new VueRouter({
routes:routes
});
new Vue({
el: '#app',
router, //5.把router实例放入到vue实例中
render: h => h(App)
})
<span>
<router-link to="/home">home</router-link>
</span>
<span>
<router-link to="/products">product</router-link>
</span>
routes.js设置
export const routes = [
{ path: '/home', component: Home },
{ path: '/products/:id', component: Products }
]
组件内获取
<script>
export default {
name: "Products.vue",
data() {
return {
id: this.$route.params.id,
}
},
};
</script>
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有