组件
引用组件,可以使用标签的方式嵌入到 < template>中
理解:组件实现的效果在页面中嵌入别的部分的页面,通常是 Header.vue,Content.vue, Bottom.vue等插件类型
路由
调用的也是页面,但是可以通过url的方式调用
vue路由的两种方式
1. 声明式 router-link (利用标签跳转,注意对应的要写router-view)
1.0 不带参数 形如:http://localhost:3000/#/home/newslist
2
3 // router.js 路由配置
4 { path: '/home/newslist', name:'newslist' component: Newslist }
5
6 <router-link to="/home/newslist">
7 <router-link :to="{name:'newslist'}">
8 <router-link :to="{path:'/home/newslist'}"> //name,path都行, 建议用name
9
10 // 注意:router-link中链接如果是'/'开始就是从根路由开始,如果开始不带'/',则从当前路由开始
11
12
1.1带参数 形如(例如从商品进入详情):http://localhost:3000/#/home/newsinfo/13
14
15 // router.js 路由配置
16 { path: '/home/newsinfo/:id', name: 'newsinfo', component: Newsinfo } //或者
17 { path: '/home/newsinfo:id', name: 'newsinfo', component: Newsinfo }
18
19 <router-link :to="'/home/newsinfo/' + item.id">
20 <router-link :to="{name:'newsinfo', params: {id:item.id}}"> //router.js用上面的
21
22 // params传参数 (类似post) 直接跟参数的形式
23 // 不配置path ,第一次可请求,刷新页面id会消失
24 // 配置path,刷新页面id会保留
25
26 // html 取参 $route.params.id
27 // script 取参 this.$route.params.id
28
29 1.2带参数 形如(例如从商品进入详情):http://localhost:3000/#/home/newsinfo?id=13
30
31 // router.js 路由配置
32 { path: '/home/newsinfo', name: 'newsinfo', component: Newsinfo }
33
34 <router-link :to="'/home/newsinfo?id=' + item.id">
35 <router-link :to="'/home/newsinfo?id=' + item.id">
36
37 // query传参数 (类似get,url后面会显示参数)
38 // 路由可不配置
39 // html 取参 $route.query.id
40 // script 取参 this.$route.query.id
2. 编程式:this.$router.push() (点击事件传id,函数里面接收调用)
1. 不带参数 形如:http://localhost:3000/#/home/newslist
// router.js 路由配置
{ path: '/home', name:'home' component: Home }
this.$router.push('/home')
this.$router.push({name:'home'})
this.$router.push({path:'/home'})
2.1. params传参 形如(例如从商品进入详情):http://localhost:3000/#/home/13
// router.js 路由配置
{ path: '/home/:id', name: 'home', component: Home } //或者
{ path: '/home:id', name: 'home', component: Home }
this.$router.push('/home/' + id)
this.$router.push({name:'home',params: {id:id}}) // 只能用 name
// params传参数 (类似post) 直接跟参数的形式
// 不配置path ,第一次可请求,刷新页面id会消失
// 配置path,刷新页面id会保留
// html 取参 $route.params.id
// script 取参 this.$route.params.id
2.2. query传参 形如(例如从商品进入详情):http://localhost:3000/#/home?id=13
// router.js 路由配置
{ path: '/home', name: 'home', component: Home }
this.$router.push('/home?id=' + id)
this.$router.push({name:'home',query: {id:id}})
this.$router.push({path:'/home',query: {id:id}})
// query传参数 (类似get,url后面会显示参数)
// html 取参 $route.query.id
// script 取参 this.$route.query.id