在vue-cli中使用路由


1.首先npm中是否有vue-router

一般在vue-cli的時候就已經下載好了依賴包了

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'//需要跳轉的頁面 給組件重新命名

Vue.use(Router)

export default new Router({
  routes: [
    {//首頁
      path: '/',
      name: 'Hello',
      component: Hello
    },
    {//需要跳轉的頁面
    	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>//跳轉首頁
    <router-link to="/test">test</router-link>//跳轉新頁面
    </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')//實例掛載到元素中

  兩個頁面的組件

這樣的話,基本的路由設置就好了,可以按照正常的npm run dev運行這個項目了

另外還有嵌套 自定義多種路由

具體的路由內容可以查看:https://router.vuejs.org/zh-cn/installation.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM