欢迎关注我的公众号:「测试游记」
嵌套路由
场景:父页面打开,再打开子页面
借鉴官方示例:
https://github.com/vuejs/vue-router/blob/dev/examples/nested-routes/app.js

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| const router = new VueRouter({ mode: 'history', base: __dirname, routes: [ {path: '/', redirect: '/parent'}, { path: '/parent', component: Parent, children: [ {path: '', component: Default}, {path: 'foo', component: Foo}, {path: 'bar', component: Bar}, {path: '/baz', component: Baz}, { path: 'qux/:quxId', component: Qux, children: [ {path: 'quux', name: 'quux', component: Quux}, {path: 'quuy', name: 'quuy', component: Quuy} ] }, {path: 'quy/:quyId', component: Quy}, {name: 'zap', path: 'zap/:zapId?', component: Zap} ] } ] })
|
修改/src/router/index.js
1 2 3 4 5 6 7 8 9
| { path: '/login', component: Login, children: [ {path: '', component: Login}, {path: '/projects_list2', component: ProjectsList}, {path: 'projects_list2', component: ProjectsList} ] },
|
在 /src/components/Login.vue的template中添加以下内容
1 2
| <p>=====================</p> <router-view></router-view>
|
输入:http://localhost:8080/login会显示两个输入内容
这是因为子路由中有一个空的默认路由:{path: '', component: Login},所以父路由加载一次,子路由加载一次

输入:http://localhost:8080/projects_list2会显示登录部分和项目部分
但是它使用的是{path: '/projects_list2', component: ProjectsList}
输入:http://localhost:8080/login/projects_list2也会显示一样的内容
但它使用的是{path: 'projects_list2', component: ProjectsList}
两者区别是有无/

axios
安装方式:
测试接口:https://dog.ceo/api/breeds/image/random

修改/src/components/HelloWorld.vue
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| <script> import axios from 'axios'
export default { name: 'HelloWorld', props: { msg: String, title: Number, }, mounted() { axios.get('https://dog.ceo/api/breeds/image/random') .then(function (response) { console.log(response); console.log(response.data); }) .catch(function (err) { console.log(err); }) } } </script>
|

/src/components/HelloWorld.vue
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| <template> <div class="hello"> <h1>{{ msg }}</h1> <h2>{{ title }}</h2> <el-image :src="url" fit="cover"></el-image> </div> </template>
<script> import axios from 'axios' export default { name: 'HelloWorld', props: { msg: String, title: Number, }, data() { return { url: '', } }, mounted() { axios.get('https://dog.ceo/api/breeds/image/random') .then(response => { console.log(response); console.log(response.data); this.url = response.data.message; }) .catch(function (err) { console.log(err); }); } } </script>
<style scoped> h3 { margin: 40px 0 0; } ul { list-style-type: none; padding: 0; } li { display: inline-block; margin: 0 10px; } a { color: #42b983; } </style>
|

代码封装
新建/src/api/api.js
1 2 3 4 5 6 7 8
| import axios from 'axios'
var host = 'https://dog.ceo';
export const dogs = () => { return axios.get(`${host}/api/breeds/image/random`) };
|
修改HelloWorld.vue
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| import {dogs} from '../api/api.js'
mounted() { dogs() .then(response => { console.log(response); console.log(response.data); this.url = response.data.message; }) .catch(function (err) { console.log(err); }); }
|
slot插槽
默认情况下,在子组件开始标签和结束标签中间添加的内容会忽略
在/src/components/greeting.vue的template中增加以下内容
1 2 3
| <hello-world> <p>这是Hello-world子组件</p> </hello-world>
|
如果需要展示出来需要在/src/components/HelloWorld.vue中添加:
<slot></slot>也就是插槽
如果在slot中写入内容,那外部不使用会使用默认的:
<slot><p>喵喵喵</p></slot>
1 2 3 4 5
| # greeting.vue <hello-world></hello-world>
# HelloWorld.vue <slot><p>喵喵喵</p></slot>
|

1 2 3 4 5
| # greeting.vue <p>这是Hello-world子组件</p>
# HelloWorld.vue <slot><p>喵喵喵</p></slot>
|

命名插槽
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| # greeting.vue <hello-world> <p slot="part1">这是Hello-world子组件</p> <template v-slot="part2"> <template #part2> <p>这是part2</p> </template> </hello-world>
# HelloWorld.vue <slot name="part1"><p>汪汪汪</p></slot> <slot name="part2"><p>咳咳咳</p></slot>
|

插槽作用域
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| # greeting.vue <hello-world> <p slot="part4" slot-scope="sope"> {{sope.user}}:这是Hello-world子组件 </p> <template #part4="sope"> <p>{{sope.user}}:这是part4</p> </template> <template #part4="{user}"> <p>{{user}}:这是part4</p> </template> </hello-world>
# HelloWorld.vue <slot name="part4" :user="username"></slot>
|
