首页 / VUE / Vue-Router-编程式导航
Vue-Router-编程式导航
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Vue-Router-编程式导航,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2290字,纯文字阅读大概需要4分钟。
内容图文
编程式导航
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="http://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
<title>Vue-Router</title>
</head>
<body>
<div id="box">
<p>
<button @click="jump('home')">home</button>
<button @click="jump('news')">news</button>
<button @click="replaceJump('home')">replace-home</button>
<button @click="replaceJump('news')">replace-news</button>
<button @click="goJump(1)">前进一步</button>
<button @click="goJump(-1)">后退一步</button>
<button @click="goJump(6)">N步</button>
</p>
<router-view></router-view>
</div>
<!-- 模板抽离 -->
<template id="login">
<div class="login">
{{msg}}
</div>
</template>
<template id="home">
<div class="home">
{{msg}}
</div>
</template>
<template id="news">
<div class="news">
{{msg}}
</div>
</template>
<template id="com404">
<div class="com404">
{{msg}}
</div>
</template>
<script type="text/javascript">
// 1.定义路由组件
const Login = {
data() {
return {
msg: "这里是login组件"
}
},
template: "#login"
};
const Home = {
data() {
return {
msg: "这里是home组件"
}
},
template: "#home"
};
const News = {
data() {
return {
msg: "这里是news组件"
}
},
template: "#news"
};
const Com404 = {
data() {
return {
msg: "这里是com404组件"
}
},
template: "#com404"
};
// 2.定义路由
const routes = [
{//匹配根路径
path: '/',
name: 'Login',
component: Login,
},
{
path: '/home',
name: 'Home',
component: Home,
},
{
path: '/news',
name: 'News',
component: News,
},
{//捕获所有路由或 404 Not found 路由
path: '*',
name: 'Com404',
component: Com404,
}
];
// 3. 创建 router 实例,然后传 `routes` 配置
const router = new VueRouter({
// mode: 'history',
routes // (缩写)相当于 routes: routes
});
// 4. 创建和挂载根实例。
const app = new Vue({
el: "#box",
data() {
return {}
},
methods: {
jump(path) {
// 字符串
// this.$router.push(path);
// 对象
// this.$router.push({ path: path })
// 命名的路由
this.$router.push({ name: path[0].toLocaleUpperCase() + path.slice(1) })
},
replaceJump(path) {
// 字符串
this.$router.replace(path);
// 对象
// this.$router.replace({ path: path })
// 命名的路由
// this.$router.push({ name: path[0].toLocaleUpperCase() + path.slice(1) })
},
goJump(speed) {
this.$router.go(speed);
}
},
router
});
</script>
</body>
</html>
内容总结
以上是互联网集市为您收集整理的Vue-Router-编程式导航全部内容,希望文章能够帮你解决Vue-Router-编程式导航所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。