詳解在vue-cli中使用路由
1.首先npm中是否有vue-router
一般在vue-cli的時候就已經(jīng)下載好了依賴包了
2.使用vue的話正常的需要涉及這幾個文件
demo/src/router/index.js
import Vue from 'vue'
import Router from 'vue-router'
import Hello from '../components/Hello'//首頁
import Test from '../components/test'//需要跳轉(zhuǎn)的頁面 給組件重新命名
Vue.use(Router)
export default new Router({
routes: [
{//首頁
path: '/',
name: 'Hello',
component: Hello
},
{//需要跳轉(zhuǎn)的頁面
path:'/test',
name:'test',
component:Test//組件名字
}
]
})
demo/src/app.vue
<template>
<div id="app">
<img src="./assets/logo.png">
<p>
<router-link to="/home">home</router-link>//跳轉(zhuǎn)首頁
<router-link to="/test">test</router-link>//跳轉(zhuǎn)新頁面
</p>
<router-view></router-view>//頁面渲染放置的部分
</div>
</template>
<script>
export default {
name: 'app'
}
</script>
<style>
#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>
demo/src/main.js
import Vue from 'vue'
import App from './App'
import router from './router'
Vue.config.productionTip = false
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
template: '<App/>',
components: { App }
}).$mount('#app')//實例掛載到元素中
兩個頁面的組件

這樣的話,基本的路由設(shè)置就好了,可以按照正常的npm run dev運行這個項目了
另外還有嵌套 自定義多種路由
具體的路由內(nèi)容可以查看:https://router.vuejs.org/zh-cn/installation.html
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用live-server快速搭建本地服務(wù)器+自動刷新的方法
下面小編就為大家分享一篇使用live-server快速搭建本地服務(wù)器+自動刷新的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03
關(guān)于Element上傳組件beforeUpload上傳前限制失效問題
這篇文章主要介紹了Element上傳組件beforeUpload上傳前限制失效問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03
vue數(shù)據(jù)操作之點擊事件實現(xiàn)num加減功能示例
這篇文章主要介紹了vue數(shù)據(jù)操作之點擊事件實現(xiàn)num加減功能,結(jié)合實例形式分析了vue.js事件響應(yīng)及數(shù)值運算相關(guān)操作技巧,需要的朋友可以參考下2019-01-01

