vue路由教程之靜態(tài)路由
前言
vue的單頁面應(yīng)用是基于路由和組件的,路由用于設(shè)定訪問路徑,并將路徑和組件映射起來。傳統(tǒng)的頁面應(yīng)用,是用一些超鏈接來實現(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)會被渲染成一個 `<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. 如果使用模塊化機制編程,導(dǎo)入 Vue 和 VueRouter,要調(diào)用 Vue.use(VueRouter)
// 1. 定義(路由)組件。
// 可以從其他文件 import 進來
const Foo = { template: '<div>foo</div>' }
const Bar = { template: '<div>bar</div>' }
// 2. 定義路由
// 每個路由應(yīng)該映射一個組件。 其中"component" 可以是
// 通過 Vue.extend() 創(chuàng)建的組件構(gòu)造器,
// 或者,只是一個組件配置對象。
// 我們晚點再討論嵌套路由。
const routes = [
{ path: '/foo', component: Foo },
{ path: '/bar', component: Bar }
]
// 3. 創(chuàng)建 router 實例,然后傳 `routes` 配置
// 你還可以傳別的配置參數(shù), 不過先這么簡單著吧。
const router = new VueRouter({
routes // (縮寫)相當(dāng)于 routes: routes
})
// 4. 創(chuàng)建和掛載根實例。
// 記得要通過 router 配置參數(shù)注入路由,
// 從而讓整個應(yīng)用都有路由功能
const app = new Vue({
router// (縮寫)相當(dāng)于 router: router
1
}).$mount('#app') // 現(xiàn)在,應(yīng)用已經(jīng)啟動了!
上例中,路由映射表實例名為routes,在創(chuàng)建路由對象時可以縮寫,如果不叫routes,比如叫routesa,則創(chuàng)建路由對象時必須寫routes:routesa
創(chuàng)建vue對象時,路由對象名也一樣,如果不叫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. 如果使用模塊化機制編程,導(dǎo)入 Vue 和 VueRouter,要調(diào)用 Vue.use(VueRouter)
// 1. 定義(路由)組件。
// 可以從其他文件 import 進來
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. 定義路由
// 每個路由應(yīng)該映射一個組件。 其中"component" 可以是
// 通過 Vue.extend() 創(chuàng)建的組件構(gòu)造器,
// 或者,只是一個組件配置對象。
// 我們晚點再討論嵌套路由。
const routes = [
{ path: '/foo', component: Foo },
{ path: '/bar', component: Bar }
]
// 3. 創(chuàng)建 router 實例,然后傳 `routes` 配置
// 你還可以傳別的配置參數(shù), 不過先這么簡單著吧。
const router = new VueRouter({
routes // (縮寫)相當(dāng)于 routes: routes
})
// 4. 創(chuàng)建和掛載根實例。
// 記得要通過 router 配置參數(shù)注入路由,
// 從而讓整個應(yīng)用都有路由功能
const app = new Vue({
router // (縮寫)相當(dāng)于 router: router
}).$mount('#app') // 現(xiàn)在,應(yīng)用已經(jīng)啟動了!
</script>
</html>
進去的時候打網(wǎng)址
xxx/xx.html#/foo 或 xxx/xx.html#/bar
就可以實現(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í)價值,謝謝大家對腳本之家的支持。
相關(guān)文章
vue如何根據(jù)不同的環(huán)境使用不同的接口地址
這篇文章主要介紹了vue如何根據(jù)不同的環(huán)境使用不同的接口地址問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04
vue?this.$refs.xxx報錯undefined問題及解決
這篇文章主要介紹了vue?this.$refs.xxx報錯undefined問題及解決,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03
Vue瀏覽器緩存sessionStorage+localStorage+Cookie區(qū)別解析
這篇文章主要介紹了Vue瀏覽器緩存sessionStorage+localStorage+Cookie區(qū)別解析,本文通過示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-09-09
express+vue+mongodb+session 實現(xiàn)注冊登錄功能
這篇文章主要介紹了express+vue+mongodb+session 實現(xiàn)注冊登錄,本文通過實例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2018-12-12

