【21】vue.js — 路由
vue.js中的路由是根據不同路徑來展示不同的頁面效果
路由入門
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" src="js/vue.js" ></script>
<script src="js/vue-router.js"></script>
</head>
<body>
<div id="box">
<ul>
<li>
<a v-link="{path:'/home'}">主頁</a>
</li>
<li>
<a v-link="{path:'/news'}">新聞</a>
</li>
</ul>
<div>
<router-view></router-view>
</div>
</div>
</body>
<script>
//1.準備一個根元件
var App = Vue.extend();
//2.Home News元件都準備
var Home = Vue.extend({
template: '<h3>我是主頁</h3>'
});
var News = Vue.extend({
template: '<h3>我是新聞</h3>'
});
//3.準備路由
var router = new VueRouter();
//4.關聯
router.map({
'home': {
component: Home
},
'news': {
component: News
}
});
//5.啟動路由
router.start(App,'#box');
</script>
</html>
多層路由
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script src="js/vue.js"></script>
<script type="text/javascript" src="js/vue-router.js" ></script>
<style>
</style>
</head>
<body>
<div id="box">
<ul>
<li>
<a v-link="{path:'/home'}">主頁</a>
</li>
<li>
<a v-link="{path:'/news'}">新聞</a>
</li>
</ul>
<div>
<router-view></router-view>
</div>
</div>
<template id="home">
<h3>我是主頁</h3>
<div>
<a v-link="{path:'/home/login'}">登入</a>
<a v-link="{path:'/home/reg'}">註冊</a>
</div>
<div>
<router-view></router-view>
</div>
</template>
<template id="news">
<h3>我是新聞</h3>
</template>
</body>
<script>
//1.準備一個元件
var App = Vue.extend();
//2. Home News元件都準備
var Home = Vue.extend({
template: '#home'
});
var News = Vue.extend({
template: '#news'
});
//3.準備路由
var router=new VueRouter();
//4.關聯
router.map({
'home': {
component: Home,
subRoutes: {
'login': {
component: {
template: '<strong>我是登入資訊<strong>'
}
},
'reg': {
component: {
template: '<strong>我是註冊資訊</strong>'
}
}
}
},
'news':{
component:News
}
});
//5. 啟動路由
router.start(App,'#box');
//6. 跳轉
router.redirect({
'/':'home'
});
</script>
</html>
vue中路由傳遞引數
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script src="js/vue.js"></script>
<script type="text/javascript" src="js/vue-router.js" ></script>
</head>
<body>
<div id="box">
<ul>
<li>
<a v-link="{path:'/home'}">主頁</a>
</li>
<li>
<a v-link="{path:'/news'}">新聞</a>
</li>
</ul>
<div>
<router-view></router-view>
</div>
</div>
<template id="home">
<h3>我是主頁</h3>
<div>
<a v-link="{path:'/home/login'}">登入</a>
<a v-link="{path:'/home/reg'}">註冊</a>
</div>
<div>
<router-view></router-view>
</div>
</template>
<template id="news">
<h3>我是新聞</h3>
<div>
<a v-link="{path:'/news/detail/001'}">新聞001</a>
<a v-link="{path:'/news/detail/002'}">新聞002</a>
</div>
<router-view></router-view>
</template>
<template id="detail">
{{$route.params | json}}
</template>
</body>
<script>
//1.準備一個元件
var App = Vue.extend();
//2. Home News元件都準備
var Home = Vue.extend({
template: '#home'
});
var News = Vue.extend({
template: '#news'
});
var Detail = Vue.extend({
template: '#detail'
});
//3.準備路由
var router=new VueRouter();
//4.關聯
router.map({
'home': {
component: Home,
subRoutes: {
'login': {
component: {
template: '<strong>我是登入資訊<strong>'
}
},
'reg': {
component: {
template: '<strong>我是註冊資訊</strong>'
}
}
}
},
'news':{
component:News,
subRoutes: {
'/detail/:id': {
component: Detail
}
}
}
});
//5. 啟動路由
router.start(App,'#box');
//6. 跳轉
router.redirect({
'/':'home'
});
</script>
</html>
相關文章
- 邏輯Vue.js 路由Vue.js路由
- 開心檔之Vue.js 路由Vue.js路由
- 後臺管理系統vue.js路由Vue.js路由
- Vue.js 2.x筆記:路由Vue Router(6)Vue.js筆記路由
- Vue.js中,如何自己維護路由跳轉記錄?Vue.js路由
- vue.js除了動態路由,前端許可權還可以這麼玩Vue.js路由前端
- 關於vue.js如何根據網站路由判斷頁面主題色教程詳解Vue.js網站路由
- 邂逅VUE.JSVue.js
- 21
- Angular路由——子路由Angular路由
- Vue.js + Docker 部署Vue.jsDocker
- Vue.js – 起手式Vue.js
- vue.js原始碼Vue.js原始碼
- Vue.js學習Vue.js
- Angular路由——輔助路由Angular路由
- Angular路由——路由基礎Angular路由
- 前端路由(手寫路由)前端路由
- Angular路由——路由守衛Angular路由
- 3/21
- 5/21
- OSPF路由 與 ISIS路由 與路由學習對比路由
- 21億Q綁查詢21億Q綁查詢21億Q綁查詢21億Q綁查詢21億Q綁查詢
- Vue.js大總結Vue.js
- Vue.js初體驗Vue.js
- 說說 Vue.js 元件Vue.js元件
- vue.js元件開發Vue.js元件
- Vue.js中的MVVMVue.jsMVVM
- 1.引入vue.jsVue.js
- Vue.js - Day1Vue.js
- VUE.JS 面試題Vue.js面試題
- Vue.js的基礎Vue.js
- Vue.js快速入門Vue.js
- Vue.js 2.6嚐鮮Vue.js
- vue.js生命週期Vue.js
- vue.js繫結classVue.js
- vue.js語法糖Vue.js
- 靜態路由和動態路由路由
- vue21Vue
- 4-21