詳解在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)文章
vue-infinite-loading2.0 中文文檔詳解
本篇文章主要介紹了vue-infinite-loading2.0 中文文檔詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04
vue實現(xiàn)登錄后頁面跳轉(zhuǎn)到之前頁面
本文給大家分享了vue實現(xiàn)登錄后頁面跳轉(zhuǎn)到之前頁面的一個功能,有這方便需要的朋友學(xué)習(xí)參考下吧。2018-01-01
vue學(xué)習(xí)筆記之Vue中css動畫原理簡單示例
這篇文章主要介紹了vue學(xué)習(xí)筆記之Vue中css動畫原理,結(jié)合簡單實例形式分析了Vue中css樣式變換動畫效果實現(xiàn)原理與相關(guān)操作技巧,需要的朋友可以參考下2020-02-02

