模板抽离
我们已经学习过了Vue模板的另外定义形式,使用。
首页
新闻
然后js里定义路由组件的时候:
// 1. 定义(路由)组件。
const Home = { template: '#home' };
const News = { template: '#news' };
路由嵌套
实际应用界面,通常由多层嵌套的组件组合而成。
比如,我们 “首页”组件中,还嵌套着 “登录”和 “注册”组件,那么URL对应就是/home/login和/home/reg。
首页
登录
注册
这是访问/home后的模板,其中我们需要把/home/login和/home/reg渲染进来。
完成上面代码后,HTML结构如下图:
登录和注册2个组件
登录界面
注册界面
//定义路由组件
const Login = { template: '#login' };
const Reg = { template: '#reg' };
3.定义路由
// 2. 定义路由
const routes = [
{ path: '/', redirect: '/home' },
{
path: '/home',
component: Home,
children:[
{ path: '/home/login', component: Login},
{ path: '/home/reg', component: Reg}
]
},
{ path: '/news', component: News}
]
注意我们在home路由配置了它的children。这就是嵌套路由。
4.案例全部代码如下:
home news 首页 登录 注册 新闻 登录界面 注册界面
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。



