Home>Article>Web Front-end> 9 Vue tips to improve development efficiency and performance
This article will share with you several tips that I have learned during my study ofVueto improve development efficiency and performance. I hope it will be helpful to everyone!
$attrs
and$listeners
## skillfully #$attrsis used to record all parameters passed from the parent component to the child component that are not captured by
propsand are not
classand
style, but
$listenersis used to record all events passed in from the parent component without the
.nativemodifier. (Learning video sharing:
vuejs video tutorial)
Vue.component('child', { props: ['title'], template: 'is in{{ title }}
' }) new Vue({ data:{a:1,title:'title'}, methods:{ handleClick(){ // ... }, handleChange(){ // ... } }, template:'', })
The value of
is
{a:1,b:"1"}
The value is
{change: handleChange}
$attrsand
$listenersfor component communication. It is more efficient when used in sub-encapsulated components, such as:
Vue.component("custom-dialog", { // 通过v-bind="$attrs"和v-on="$listeners"把父组件传入的参数和事件都注入到el-dialog实例上 template: 'Another example:', }); new Vue({ data: { visible: false }, // 这样子就可以像在el-dialog一样用visible控制custom-dialog的显示和消失 template: ' ', });
Vue.component("custom-select", { template: ``, }); new Vue({ data: { value: "" }, // v-model在这里其实是v-bind:value和v-on:change的组合, // 在custom-select里,通过v-bind="$attrs" v-on="$listeners"的注入, // 把父组件上的value值双向绑定到custom-select组件里的el-select上,相当于 // 与此同时,在custom-select注入的size变量也会通过v-bind="$attrs"注入到el-select上,从而控制el-select的大小 template: ' ', });
$porpsis used to record all the props passed from the parent component to the child component that are captured by
propsand are not
classand
styleparameters. For example,
Vue.component('child', { props: ['title'], template: 'is in{{ title }}
' }) new Vue({ data:{a:1,title:'title'}, methods:{ handleClick(){ // ... }, handleChange(){ // ... } }, template:'', })
, and the value of
$propsis
{title:'title'}.
$propscan be used when the
propsdefined by the self component and the grandchild component are the same, such as:
Vue.component('grand-child', { props: ['a','b'], template: '{{ a + b}}
' }) // child和grand-child都需要用到来自父组件的a与b的值时, // 在child中可以通过v-bind="$props"迅速把a与b注入到grand-child里 Vue.component('child', { props: ['a','b'], template: `{{a}}加{{b}}的和是:` }) new Vue({ template:'', })
vuecomponents is that they are non-responsive. It does not listen to any data and has no instances (so no state, meaning there is no life cycle such as
created,
mounted). The advantage is that since it is just a function, the rendering overhead is much lower.
vue-devtoolsfor debugging in the environment. Just change the
Vue.config.devtoolsconfiguration, as shown below:
// 务必在加载 Vue 之后,立即同步设置以下内容 // 该配置项在开发版本默认为 `true`,生产版本默认为 `false` Vue.config.devtools = true;We can pass the detection Use the user role information in
cookieto decide whether to enable this configuration item, thereby improving the convenience of online bug finding.
Vue
to assign the result returned by a higher-order function, for example:
searchfunction is assigned the result returned by
debounce, which is the request function with anti-shake function. This method can avoid us having to write the anti-shake logic ourselves in the component.
sandbox. You can click on it to see the difference between themethodprocessed by the high-order function and the original
method. As shown below:
methodcan also be defined as a
generator, if we have a function that needs The order is emphasized during execution, and variables need to be defined indatato record the last status, then you can consider using a generator.
call time. Thiscall timerequires every second Update once, that is, the function to obtaincall timeneeds to be executed once per second. If written as a normal function, the variable recording the time needs to be stored indata. But if you use a generator, it can be solved very cleverly, as shown below:
The page effect is as follows:{{ timeFormat }}
Code address: https://codesandbox.io/s/jovial-williams-nkouf?file=/src/App.vue
But it should be noted that method cannot be an arrow function
Note that arrow functions should not be used to definemethod
functions (for example,
plus: () => this.a). The reason is that arrow functions are bound to the context of the parent scope, so
thiswill not point to the Vue instance as expected,
this.awill be
undefined.
handler# of a certain variable in 虽然 这样子就可以在初始值由于需求需要更改时,只在 这里再举一个场景:一个 每次 以上代码放在sanbox里 如果要重置 只渲染元素和组件一次。随后的重新渲染,元素/组件及其所有的子节点将被视为静态内容并跳过。这可以用于优化更新性能。 对于部分在首次渲染后就不会再有响应式变化的元素,可以用 如果上述例子中的变量 如果想监听子组件的生命周期时,可以像下面例子中这么做: 这样的写法可以用于处理加载第三方的初始化过程稍漫长的子组件时,我们可以加 初次之外 像上面这种写法就保证了逻辑的统一,遵循了单一职责原则。 The above is the detailed content of 9 Vue tips to improve development efficiency and performance. For more information, please follow other related articles on the PHP Chinese website!watch
## Call multiple operations, as shown below:<script> export default { data() { return { value: "", }; }, methods: { fn1() {}, fn2() {}, }, watch: { value: { handler() { fn1(); fn2(); }, immediate: true, deep: true, }, }, }; </script>
fn1
和fn2
都需要在value
变动的时候调用,但两者的调用时机可能不同。fn1
可能仅需要在deep
为false
的配置下调用既可。因此,Vue
在watch
的值添加了Array
类型来针对上面所说的情况,如果用watch
为Array
的写法处理可以写成下面这种形式:7. 妙用
$options
$options
是一个记录当前Vue
组件的初始化属性选项。通常开发中,我们想把data
里的某个值重置为初始化时候的值,可以像下面这么写:this.value = this.$options.data().value;
data
中更改即可。el-dialog
中有一个el-form
,我们要求每次打开el-dialog
时都要重置el-form
里的数据,则可以这么写:
el-dialog
打开之前都会调用其@open
中的方法initForm
,从而重置form
值到初始值。如下效果所示:data
里的所有值,可以像下面这么写:Object.assign(this.$data, this.$options.data()); // 注意千万不要写成下面的样子,这样子就更改this.$data的指向。使得其指向另外的与组件脱离的状态 this.$data = this.$options.data();
8. 妙用 v-pre,v-once
v-pre
v-pre
用于跳过被标记的元素以及其子元素的编译过程,如果一个元素自身及其自元素非常打,而又不带任何与Vue
相关的响应式逻辑,那么可以用v-pre
标记。标记后效果如下:v-once
v-once
属性去标记,如下:options
很大且不会再有响应式变化,那么如例子中用上v-once
对性能有提升。9. 妙用 hook 事件
loading
动画,等到子组件加载完毕,到了mounted
生命周期时,把loading
动画移除。hook
还有一个常用的写法,在一个需要轮询更新数据的组件上,我们通常在created
里开启定时器,然后在beforeDestroy
上清除定时器。而通过hook
,开启和销毁定时器的逻辑我们都可以在created
里实现: