手機賺錢怎么賺,給大家推薦一個手機賺錢APP匯總平台:手指樂(http://www.szhile.com/),辛苦搬磚之余用閑余時間動動手指,就可以日賺數百元
- route-link是在html中靜態定義的,也可以在代碼中動態跳轉:
<!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. 如果使用模塊化機制編程,導入 Vue 和 VueRouter,要調用 Vue.use(VueRouter)
// 1. 定義(路由)組件。
// 可以從其他文件 import 進來
// const Foo = { template: '<div @onclick="pushtest" href="">Go to Bar</a>' }
// const Bar = { template: '<div @onclick="pushtest" href="">Go to Foo</a>' }
const Foo = Vue.extend({
template: '<a @click="pushtest" href="javascript:void(0)">Navigate to bar</a>',
methods: {
pushtest() {
//alert("bar");
this.$router.push({ name: 'bar' });
//alert("fdas");
},
},
});
const Bar = Vue.extend({
template: '<a @click="pushtest" href="javascript:void(0)">Navigate to foo</a>',
methods: {
pushtest() {
//alert("foo");
this.$router.push({ name: 'foo' });
//alert("fdas");
},
},
});
// 2. 定義路由
// 每個路由應該映射一個組件。 其中"component" 可以是
// 通過 Vue.extend() 創建的組件構造器,
// 或者,只是一個組件配置對象。
// 我們晚點再討論嵌套路由。
const routes = [
{ path: '/', redirect: "/bar" },
{ path: '/foo', name: "foo", component: Foo },
{ path: '/bar', name: "bar", component: Bar },
]
// 3. 創建 router 實例,然后傳 `routes` 配置
// 你還可以傳別的配置參數, 不過先這么簡單着吧。
const router = new VueRouter({
routes // (縮寫)相當於 routes: routes
})
// 4. 創建和掛載根實例。
// 記得要通過 router 配置參數注入路由,
// 從而讓整個應用都有路由功能
const app = new Vue({
router, // (縮寫)相當於 router: router
// methods: {
// pushtest:function() {
// alert("fdas");
// },
// },
watch: {
$route(to, from) {
//alert(to.path);
//document.getElementById("testzy").innerText = this.$route.params.id;
}
},
}).$mount('#app') // 現在,應用已經啟動了!
</script>
</html>
注意絕對不能寫href="",這樣執行click跳轉后,又會執行href跳轉到當前頁面
push也可以直接使用path:
this.$router.push('/foo');
- push會向history添加一條新記錄,此時后退會跳轉到前一個組件
router.replace(location)跟push功能類似,但是不會向history添加一條新記錄,不能后退
router.go(n)
這個方法的參數是一個整數,意思是在 history 記錄中向前或者后退多少步,類似 window.history.go(n)。
// 在瀏覽器記錄中前進一步,等同於 history.forward() router.go(1) // 后退一步記錄,等同於 history.back() router.go(-1) // 前進 3 步記錄 router.go(3) // 如果 history 記錄不夠用,那就默默地失敗唄 router.go(-100) router.go(100)
