欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue路由教程之靜態(tài)路由

 更新時(shí)間:2019年09月03日 14:49:44   作者:手指樂  
這篇文章主要給大家介紹了關(guān)于vue路由教程之靜態(tài)路由的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用vue具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧

前言

vue的單頁面應(yīng)用是基于路由和組件的,路由用于設(shè)定訪問路徑,并將路徑和組件映射起來。傳統(tǒng)的頁面應(yīng)用,是用一些超鏈接來實(shí)現(xiàn)頁面切換和跳轉(zhuǎn)的。在vue-router單頁面應(yīng)用中,則是路徑之間的切換,也就是組件的切換。

首先在html中,引入vue-router.js和vue.js,用router-link觸發(fā)路由跳轉(zhuǎn),router-link可以像a標(biāo)簽一樣使用和定義樣式

router-view區(qū)域是路由匹配到的組件渲染的地方

<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
 
<div id="app">
 <h1>Hello App!</h1>
 <p>
 <!-- 使用 router-link 組件來導(dǎo)航. -->
 <!-- 通過傳入 `to` 屬性指定鏈接. -->
 <!-- <router-link> 默認(rèn)會(huì)被渲染成一個(gè) `<a>` 標(biāo)簽 -->
 <router-link to="/foo">Go to Foo</router-link>
 <router-link to="/bar">Go to Bar</router-link>
 </p>
 <!-- 路由出口 -->
 <!-- 路由匹配到的組件將渲染在這里 -->
 <router-view></router-view>
</div>

然后是js代碼

首先定義路由組件,組件可以是簡單的組件(template簡單定義即可),也可是extend定義的復(fù)雜組件

接下來定義路由映射表,就是定義路徑和組件之間的映射 

然后使用路由映射表創(chuàng)建路由對象 

最后使用路由對象創(chuàng)建vue對象,并掛載到指定元素

// 0. 如果使用模塊化機(jī)制編程,導(dǎo)入 Vue 和 VueRouter,要調(diào)用 Vue.use(VueRouter)
 
// 1. 定義(路由)組件。
// 可以從其他文件 import 進(jìn)來
const Foo = { template: '<div>foo</div>' }
const Bar = { template: '<div>bar</div>' }
 
// 2. 定義路由
// 每個(gè)路由應(yīng)該映射一個(gè)組件。 其中"component" 可以是
// 通過 Vue.extend() 創(chuàng)建的組件構(gòu)造器,
// 或者,只是一個(gè)組件配置對象。
// 我們晚點(diǎn)再討論嵌套路由。
const routes = [
 { path: '/foo', component: Foo },
 { path: '/bar', component: Bar }
]
 
// 3. 創(chuàng)建 router 實(shí)例,然后傳 `routes` 配置
// 你還可以傳別的配置參數(shù), 不過先這么簡單著吧。
const router = new VueRouter({
 routes // (縮寫)相當(dāng)于 routes: routes
})
 
// 4. 創(chuàng)建和掛載根實(shí)例。
// 記得要通過 router 配置參數(shù)注入路由,
// 從而讓整個(gè)應(yīng)用都有路由功能
const app = new Vue({
 router// (縮寫)相當(dāng)于 router: router
1
}).$mount('#app') // 現(xiàn)在,應(yīng)用已經(jīng)啟動(dòng)了!

上例中,路由映射表實(shí)例名為routes,在創(chuàng)建路由對象時(shí)可以縮寫,如果不叫routes,比如叫routesa,則創(chuàng)建路由對象時(shí)必須寫routes:routesa

創(chuàng)建vue對象時(shí),路由對象名也一樣,如果不叫router,也不能縮寫

使用extend創(chuàng)建模板

var todoItem = Vue.extend({
  data: function() {
   return {
    todoData: [
     { id: 0, text: '蔬菜' },
     { id: 1, text: '奶酪' },
     { id: 2, text: '隨便其它什么人吃的東西' }
    ]
   };
  },
  template: `
  <ul>
   <li v-for='(d, i) in todoData' :key="i">
    {{ d.text }}
   </li>
  </ul>
 `,
 
 });
 
 
 Home = { template: '<div>foo</div>' }
 About = { template: '<div>bar</div>' }
 
 routes = [
  { path: '/home', component: Home },
  { path: '/about', component: todoItem }
 ]
 
 router = new VueRouter({
  routes:routes // (縮寫)相當(dāng)于 routes: routes
 });
 
 app = new Vue({
  router:router
 }).$mount('#app');

還可以這樣寫template

<!DOCTYPE html>
<!-- saved from url=(0077)https://keepfool.github.io/vue-tutorials/06.Router/basic/basic_01.html#!/home -->
<html>
 
<head>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 <title>abc</title>
 <link rel="stylesheet" >
 <link rel="stylesheet" href="./basic_01_files/custom.css" rel="external nofollow" >
</head>
 
<body>
 <div id="app">
  <div class="row">
   <div class="col-xs-offset-2 col-xs-8">
    <div class="page-header">
     <h2>Router Basic - 01</h2>
    </div>
   </div>
  </div>
  <div class="row">
   <div class="col-xs-2 col-xs-offset-2">
    <div class="list-group">
      
     <router-link class="list-group-item" to="/home">Go to Foo</router-link>
     <router-link class="list-group-item" to="/about">Go to Bar</router-link>
    </div>
   </div>
   <router-view></router-view>
  </div>
 </div>
 <template id="home">
  <div>
   <h1>Home</h1>
   <p>{{msg}}</p>
  </div>
 </template>
 
 <script src="https://cdn.bootcss.com/vue/2.4.2/vue.min.js"></script>
 <script src="https://cdn.bootcss.com/vue-router/2.7.0/vue-router.min.js"></script>
 <script>
  
 var todoItem = Vue.extend({
  data: function() {
   return {
    todoData: [
     { id: 0, text: '蔬菜' },
     { id: 1, text: '奶酪' },
     { id: 2, text: '隨便其它什么人吃的東西' }
    ]
   };
  },
  template: `
  <ul>
   <li v-for='(d, i) in todoData' :key="i">
    {{ d.text }}
   </li>
  </ul>
 `,
 
 });
 
 var t_test = Vue.extend({
  data:function(){
   return {
    msg:"hello,test"
   };
  },
  template:"#home"
 
  }
 
 );
 
 
 
 // Home = { template: '<div>foo</div>' }
 // About = { template: '<div>bar</div>' }
 
 routes = [
  { path: '/home', component: t_test },
  { path: '/about', component: todoItem }
 ]
 
 router = new VueRouter({
  routes: routes // (縮寫)相當(dāng)于 routes: routes
 });
 
 app = new Vue({
  router: router
 }).$mount('#app');
 </script>
</body>
 
</html>

如果不需要固定的導(dǎo)航鏈接,可以把router-link放在模板里面:

<!DOCTYPE html>
<html lang="en">
 
<head>
 <meta charset="UTF-8">
 <title>abc</title>
 <script src="https://unpkg.com/vue/dist/vue.js"></script>
 <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
</head>
 
<body>
 <div id="app">
  <h1>Hello App!</h1>
   
  <!-- 路由出口 -->
  <!-- 路由匹配到的組件將渲染在這里 -->
  <router-view>
   
  </router-view>
 </div>
</body>
<script type="text/javascript">
// 0. 如果使用模塊化機(jī)制編程,導(dǎo)入 Vue 和 VueRouter,要調(diào)用 Vue.use(VueRouter)
 
// 1. 定義(路由)組件。
// 可以從其他文件 import 進(jìn)來
const Foo = { template: '<router-link to="/bar">Go to Bar</router-link>' }
const Bar = { template: '<router-link to="/foo">Go to Foo</router-link>' }
 
// 2. 定義路由
// 每個(gè)路由應(yīng)該映射一個(gè)組件。 其中"component" 可以是
// 通過 Vue.extend() 創(chuàng)建的組件構(gòu)造器,
// 或者,只是一個(gè)組件配置對象。
// 我們晚點(diǎn)再討論嵌套路由。
const routes = [
 { path: '/foo', component: Foo },
 { path: '/bar', component: Bar }
]
 
// 3. 創(chuàng)建 router 實(shí)例,然后傳 `routes` 配置
// 你還可以傳別的配置參數(shù), 不過先這么簡單著吧。
const router = new VueRouter({
 routes // (縮寫)相當(dāng)于 routes: routes
})
 
// 4. 創(chuàng)建和掛載根實(shí)例。
// 記得要通過 router 配置參數(shù)注入路由,
// 從而讓整個(gè)應(yīng)用都有路由功能
const app = new Vue({
 router // (縮寫)相當(dāng)于 router: router
 
}).$mount('#app') // 現(xiàn)在,應(yīng)用已經(jīng)啟動(dòng)了!
</script>
 
</html>

進(jìn)去的時(shí)候打網(wǎng)址

xxx/xx.html#/foo 或 xxx/xx.html#/bar

就可以實(shí)現(xiàn)foo和bar模板之間互相跳轉(zhuǎn)

也可以設(shè)置默認(rèn)路由:

const routes = [
 { path: '/', component: Bar },
 { path: '/foo', component: Foo },
 { path: '/bar', component: Bar },
 
]

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對腳本之家的支持。

相關(guān)文章

  • 解讀vant的Uploader上傳問題

    解讀vant的Uploader上傳問題

    這篇文章主要介紹了解讀vant的Uploader上傳問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • 關(guān)于vite+vue3打包部署問題

    關(guān)于vite+vue3打包部署問題

    這篇文章主要介紹了關(guān)于vite+vue3打包部署問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • vue如何根據(jù)不同的環(huán)境使用不同的接口地址

    vue如何根據(jù)不同的環(huán)境使用不同的接口地址

    這篇文章主要介紹了vue如何根據(jù)不同的環(huán)境使用不同的接口地址問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • vue3組件銷毀的具體實(shí)現(xiàn)

    vue3組件銷毀的具體實(shí)現(xiàn)

    組件的銷毀意味著從 DOM 中移除該組件,并清除與之相關(guān)的所有事件監(jiān)聽器和子組件,本文主要介紹了vue3組件銷毀的具體實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-08-08
  • vue?this.$refs.xxx報(bào)錯(cuò)undefined問題及解決

    vue?this.$refs.xxx報(bào)錯(cuò)undefined問題及解決

    這篇文章主要介紹了vue?this.$refs.xxx報(bào)錯(cuò)undefined問題及解決,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • Vue瀏覽器緩存sessionStorage+localStorage+Cookie區(qū)別解析

    Vue瀏覽器緩存sessionStorage+localStorage+Cookie區(qū)別解析

    這篇文章主要介紹了Vue瀏覽器緩存sessionStorage+localStorage+Cookie區(qū)別解析,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-09-09
  • vue中使用騰訊云Im的示例

    vue中使用騰訊云Im的示例

    這篇文章主要介紹了vue中使用騰訊云Im的示例,幫助大家調(diào)用對應(yīng)的api,完成自己的項(xiàng)目,感興趣的朋友可以了解下
    2020-10-10
  • express+vue+mongodb+session 實(shí)現(xiàn)注冊登錄功能

    express+vue+mongodb+session 實(shí)現(xiàn)注冊登錄功能

    這篇文章主要介紹了express+vue+mongodb+session 實(shí)現(xiàn)注冊登錄,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-12-12
  • 基于element-ui表格的二次封裝實(shí)現(xiàn)

    基于element-ui表格的二次封裝實(shí)現(xiàn)

    本文主要介紹了基于element-ui表格的二次封裝實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • Vue2.0生命周期的理解

    Vue2.0生命周期的理解

    這篇文章主要為大家介紹了Vue2.0生命周期,思考與理解“el被新創(chuàng)建的vm.$el替換”這句話,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-08-08

最新評論