溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

詳解windows下vue-cli及webpack 構建網站(四) 路由vue-router的使用

發布時間:2020-09-15 03:11:34 來源:腳本之家 閱讀:179 作者:安樹 欄目:web開發

windows下vue-cli及webpack 構建網站(一)環境安裝

 windows下vue-cli及webpack 構建網站(二)導入bootstrap樣式

windows下vue-cli及webpack 構建網站(三)使用組件 

1、本篇文章是建立在以上三篇文章的基礎上的。

2、安裝 vue-router 插件,運行cmd進入到項目目錄下面,運行以下命令:

cnpm install vue-router --save-dev 

詳解windows下vue-cli及webpack 構建網站(四) 路由vue-router的使用

3、在src文件夾下面新建一個文件夾page用于存放模板文件,然后分別在這個文件夾下面新建 index.vue、list.vue兩個文件,然后打開index.vue粘貼以下代碼:

<template> 
 
 <div class="jumbotron"> 
    <h2>這里是首頁!</h2> 
 </div> 
 
</template> 

保存之后再打開list.vue粘貼以下代碼:

<template> 
 
  <div class="list-group"> 
   <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="list-group-item active"> 
    這里是列表頁 
   </a> 
   <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="list-group-item">Dapibus ac facilisis in</a> 
   <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="list-group-item">Morbi leo risus</a> 
   <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="list-group-item">Porta ac consectetur ac</a> 
   <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" class="list-group-item">Vestibulum at eros</a> 
  </div> 
 
</template> 

好了,兩個頁面的內容都準備好了,接下來我們修改入口文件app.vue的內容吧

4、打開src文件夾下面的app.vue文件,修改代碼為

<template> 
 <div> 
 <HtmlHeader></HtmlHeader> 
   <router-view 
    class="view" 
    keep-alive 
    transition 
    transition-mode="out-in"> 
   </router-view> 
<HtmlFooter></HtmlFooter><span > </span>  
  </div> 
<span > </span> 
</template> 
 
 
<script> 
import HtmlHeader from './components/header' 
import HtmlFooter from './components/footer' 
export default { 
 components: { 
  HtmlHeader, 
  HtmlFooter 
 } 
} 
</script> 

這里用了 router-view 來把剛才新建的兩個頁面加載到這里來

修改了入口文件接下來就是要進行路由規則的配置了。

5、在src文件夾下面新建一個文件夾config用來存放路由配置,在config文件夾下面新建routes.js文件并打開,然后粘貼以下代碼并保存:

//加載模板文件 
import index from '../page/index' 
import list from '../page/list' 
//路由規則設置 
export default [ 
  { 
    path: '/', 
    component: index 
  }, 
  { 
    path: '/list', 
    component: list 
  } 
] 

現在路由配置文件也已經配置好了,我們接下來就是要打開sec文件夾下面的main.js文件設置路由使用了

6、打開main.js 文件,在頭部加入以下代碼

// 引用路由插件 
import VueRouter from 'vue-router' 
// 試用路由插件 
Vue.use(VueRouter) 
//引入路由配置文件 
import routes from './config/routes' 
// 使用配置文件規則 
const router = new VueRouter({ 
 mode: 'history', 
 base: __dirname, 
 routes: routes }) 

這個是引入路由插件并且使用,然后加載路由規則

接著把

new Vue({ 
 el: '#app', 
 template: '<App/>', 
 components: { App } 
}) 

修改為

const app = new Vue({ 
   router: router, 
   render: h => h(App) 
}).$mount('#app') 

詳解windows下vue-cli及webpack 構建網站(四) 路由vue-router的使用

設置完之后整個頁面代碼如圖

7、加載開始運行 npm run dev 查看效果吧,打開http://localhost:8080  和http://localhost:8080/list  就可以看到不同的效果了

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女