Home > Article > Web Front-end > How to correctly use vue component reuse function
This time I will show you how to correctly use the vue component reuse function, and what are the precautions for correctly using the vue component reuse function. The following is a practical case, let's take a look.
1. What is a component
Component (Component) is one of the most powerful features of Vue.js. Components can extend HTML elements, encapsulating reusable code.2. Component usage
Components need to be registered before they can be used. There are two ways of registration: global registration and local registration. 2.1 After global registration, any V ue instance can be used. For example:<p id="app1"> <my-component></my-component> </p> Vue.component('my-component',{ template: '<p>这里是组件的内容</p>' }); var app1 = new Vue({ el: '#app1' });To use this component in the parent instance, you must register it before the instance is created, and then you can use the component in the form of
p >". (And the outermost layer can only have one root
tag)
2.2 In a Vue instance, you can use the component option to register a component locally. The registered component is only valid within the scope of the instance. For example:<p id="app2"> <my-component1></my-component1> </p> var app2 = new Vue({ el: '#app2', components:{ 'my-component1': { template: '<p>这里是局部注册组件的内容</p>' } } });2.3 data must be a functionIn addition to the template option, components can also use other options like Vue instances, such as data, computed, methods, etc. But when using data, it is slightly different from the example. data must be a function, and then the data is returned.
<p id="app3"> <my-component3></my-component3> </p> Vue.component('my-component3',{ template: '<p>{{message}}</p>', data: function(){ return { message: '组件内容' } } }); var app3 = new Vue({ el: '#app3' });Generally, the returned object should not refer to an external object, because if the returned object refers to an external object, then this object is shared, and modifications by either party will be synchronized. So generally a new independent data object is returned to the component.
Supplement: vue-router component reuse issue
The component system is an important part of Vue, it can combine a complex page The abstraction is decomposed into many small, independent, reusable components, and the application is composed bycombining components, and combined with vue-router's routing function to map each component to the corresponding route , tell Vue where to render them through routing changes, and realize jump navigation between various components and pages.
Problem
When using vue-router to switch routes, it will trigger the update of the component tree and render a new component tree according to the defined route. The general switching process is as follows of:- Deactivate and remove unnecessary components- Verify the feasibility of switching
- Reuse components that have not been updated
- Enable and activate new components
{ path: 'post/:postId', name: 'post', component: resolve => require(['./components/Post.vue'],resolve) }This is the route that loads the corresponding article page through the article ID. When accessed for the first time, the Post.vue component will be rendered into the component tree, and the mounted component will be installed. When we obtain the article content through the article ID and display it on the page, when we access another article,
routing parameters:postId changes. According to our expectations, the content of the new article should be displayed, but it backfires.
What we see is still the previous article. Although the routing parameters have changed, vue-router will think that you are accessing the Post.vue component. Since the component has been rendered before, it will be copied directly. Use the previous component, and will not perform any operations in the component includinglifecycle functions such as mounted.
So what we finally see is the content of the original component. So how can we achieve the desired effect? An effective solution is introduced belowSolution
We can use the watch listener to monitor routing changes. According to the routing The parameters change to respond to the corresponding data. The specific implementation process is as follows: Define the data acquisition method首先定义一个获取文章的方法,根据文章ID从后台获取对应的文章信息。
methods: { getPost(postId) { this.$http.get(`/post/get_post/${postId}`).then((response) => { if(response.data.code === 0){ this.post = response.data.post; } }); } }
监听路由
接着是在路由切换的时候判断目标组件是否是Post.vue组件,这里可以根据定义的路由名称name实现,如果是,我们就可以从路由信息中获取目标文章ID来更新组件内容。
watch: { '$route' (to, from) { if(to.name === 'post'){ this.getPost(to.params.postId); } } }
组件初始化
这里需要注意的是,当组件首次被挂载到组件树上时,对路由的监听是无效的,这时我们需要在生命周期钩子mounted对组件进行初始化工作:
mounted() { this.getPost(this.$route.params.postId); }
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
The above is the detailed content of How to correctly use vue component reuse function. For more information, please follow other related articles on the PHP Chinese website!