【发布时间】:2019-11-16 14:51:44
【问题描述】:
我是 VueJs 的新手,正在尝试使用 Vue-route 设置 Web 应用程序,并且希望在用户导航到特定 URL 时更新 <header> 样式,无论是直接使用“URL 栏”还是“导航栏” ”。在这种情况下,我们有一个父组件,其中包含height_status 数据和模板上的一些<router-links>。
我已经使用$emit 技术完成了“导航栏”部分,它运行良好,但后来我尝试在created 生命周期挂钩上使用它,以便在/home 路由时更新标题创建但事件监听器不会到达 parent_component。
我该如何解决这个问题?有没有更好的方法来做到这一点?
请看下面的代码:
父组件.vue
<template>
<div id="app">
<router-link to="/home" @height_size_ctrl="change_height">Home</router-link>
<router-link to="/about">About us</router-link>
<router-link to="/contact">Contact us</router-link>
<header :class="height_status ? 'head-h-s' : 'head-h-m'"></header>
<router-view/>
</div>
</template>
<script>
export default {
name: "Parent_component"
},
data() {
return {
height_status: false
}
},
methods: {
change_height(h) {
this.height_status = h
}
}
}
</script>
router.js
Vue.use(Router)
export default new Router({
routes: [
{
path: '/home',
name: 'home',
component: Home
},
{
path: '/about',
name: 'about',
component: about
},
{
path: '/contact',
name: 'contact',
component: contact
}
]
})
home.vue
<template>
<h1>hello</h1>
</template>
<script>
export default {
name: 'home',
created: function(){
return this.$emit("height_size_ctrl", true)
}
}
</script>
【问题讨论】:
标签: vue.js vue-component vue-router vue-cli