首页 » SEO优化 » vuerouterphp视图技巧_25Vue 入门教程VueRouter 命名视图

vuerouterphp视图技巧_25Vue 入门教程VueRouter 命名视图

访客 2024-12-16 0

扫一扫用手机浏览

文章目录 [+]

实例演示

<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title></head><body> <div id="app"> <div> <router-link to="/index">首页</router-link> <router-link to="/article">文章</router-link> </div> <router-view></router-view> </div></body><script src="https://unpkg.com/vue/dist/vue.js"></script><script src="https://unpkg.com/vue-router/dist/vue-router.js"></script><script type="text/javascript">const Index = Vue.component('index', { template: '<div>Hello,欢迎利用慕课网学习 Vue 教程!
</div>',})const Article = Vue.component('myArticle', { template: `<ul><li>1. Vue 打算属性的学习</li><li>2. React 根本学习</li></ul>`,})const routes = [ { path: '/index', components: {default: Index} }, { path: '/article', components: {default: Article} }]const router = new VueRouter({ routes: routes})var vm = new Vue({ el: '#app', router: router, data() { return {} }})</script></html>

"运行案例" 可查看在线运行效果

vuerouterphp视图技巧_25Vue 入门教程VueRouter 命名视图

代码阐明: HTML 代码第 12-13 行,我们定义了两个跳转链接。
HTML 代码第 15 行,我们利用 <router-view></router-view> 组件来渲染匹配组件。
JS 代码第 5-7 行,我们定义了组件 Index。
JS 代码第 9-11 行,我们定义了组件 Article。
JS 代码第 13-16 行,我们定义了路由数组: - 1. 首页路由,地址为 ‘/index’,默认视图匹配组件 Index。
- 2. 文章路由,地址为 ‘/article’,默认视图匹配组件 Article。
JS 代码第 18-20 行,创建 router 实例,然后传 routes 配置。
JS 代码第 24 行,通过 router 配置参数注入路由。

vuerouterphp视图技巧_25Vue 入门教程VueRouter 命名视图
(图片来自网络侵删)
2.2 具名视图

除了利用默认视图名外,我们还可以给视图指定一个名字:

<router-view name="name"/>代码块1

实例演示

<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title></head><body> <div id="app"> <div> <router-link to="/index">首页</router-link> <router-link to="/article">文章</router-link> </div> <router-view name="view"></router-view> </div></body><script src="https://unpkg.com/vue/dist/vue.js"></script><script src="https://unpkg.com/vue-router/dist/vue-router.js"></script><script type="text/javascript">const Index = Vue.component('index', { template: '<div>Hello,欢迎利用慕课网学习 Vue 教程!
</div>',})const Article = Vue.component('myArticle', { template: `<ul><li>1. Vue 打算属性的学习</li><li>2. React 根本学习</li></ul>`,})const routes = [ { path: '/index', components: {view: Index} }, { path: '/article', components: {view: Article} }]const router = new VueRouter({ routes: routes})var vm = new Vue({ el: '#app', router: router, data() { return {} }})</script></html>

"运行案例" 可查看在线运行效果

代码阐明 我们对上述案例做一个大略的修正:

指定 <router-view /> 的视图名为 view。
定义路由信息的时候,指定视图 view 匹配对应组件。
3. 小结

本节,我们带大家学习了 VueRouter 命名视图的利用方法。
紧张知识点有以下几点:

通过 name 属性指定视图名称。
通过路由 components 指定各具名视图对应匹配的组件。
标签:

相关文章