Introduce lodash into vue

1. Install using npm tool:

npm i -save lodash \\全局安装

2. Import and use in vue

<template>
<el-cascader
 v-model="goods_cat"
 :options="cateList"
 :props="cateProps"
 @change="handleChange"
 ></el-cascader>
</template>

<script>
import _ from 'lodash' //导入loadsh插件
export default {
data() {
    return {
    goods_cat: []
        }
    },
methods:{

  add() {
        // 深拷贝lodash  cloneDeep(obj)
        const form = _.cloneDeep(goods_cat)
        form = form.join(',')
        console.log(form)
      })
    }
}
}
</script>

In the above example, I used the deep copy method in lodash. The reason for using deep copy here is:
in data, goods_cat is dynamically bound to the v-model of the cascade selector, and the goods_cat data type is an array. When When trying to print goods_cat as a string, the compiler will report an error.

The error is that the data type of goods_cat in the cascade selector is wrong, so the dynamically bound data needs to be copied and is not related to the original goods_cat.

At this time, you need to use the cloneDeep deep copy method in loadssh.
 

Guess you like

Origin blog.csdn.net/weixin_59739381/article/details/131859415