赞
踩
computed
属性用于声明性地描述一些依赖其它响应式属性的数据。当依赖的响应式属性变化时,计算属性会自动重新求值。computed
选项中声明,返回一个计算值。watch
用于观察和响应Vue实例上数据的变动。当指定的数据发生变化时,可以执行异步操作或开销较大的操作。watch
不会缓存结果。每次指定的数据发生变化时,都会执行相应的回调。watch
选项中声明,为每个需要观察的数据提供一个回调函数。methods
定义了组件的具体行为,即可以在组件的模板中调用,或在组件的其它逻辑中调用的函数。watch
类似,methods
中的函数在每次调用时都会执行,而不会缓存结果。methods
选项中声明,可以包含任何逻辑。computed
)适用于需要基于其他数据动态变化,并且需要缓存的场景。watch
)适用于需要响应数据变化,执行异步或开销较大操作的场景。methods
)适用于定义组件的具体行为,包括事件处理和业务逻辑等。
- <template>
- <div class="address-book-single">
- <van-search
- v-model="keywords"
- :placeholder="搜索"
- />
- <!-- 人员 -->
- <div v-for="(item, index) in filteredList" :key="item.id">
- {{ item.name }}
- </div>
-
- </div>
- </template>
-
- <script>
- import { Search} from 'vant'
-
- export default {
- name: 'AddressBook',
- components: {
- [Search.name]: Search
- },
- props: {
- templateId: {
- type: String,
- default: ''
- },
- version: {
- type: Number,
- default: 0
- },
- nodeNo: {
- type: String,
- default: ''
- }
- },
- data() {
- return {
- userList: [],
- keywords: '', // 搜索名称
- //不需要filterdeList: [],
- /*在computed选项中定义计算属性时,不需要显式地在组件的data函数中声明,
- 因为它不是组件的原始状态数据,而是基于组件状态数据计算得出的结果。*/
- }
- },
-
- computed: {
- filteredList(){
- return this.keywords ? this.userList.filter(item=>
- item.name.toLowerCase().includes(this.keywords.toLowerCase())) : this.userList
- }
- },
-
- mounted() {
- this.getUsersList()
- },
-
- methods: {
- async getUsersList() {
- this.loadUser = true
- this.showLoading()
- let { data } = await mine.approvalsApi.getNodeUsers({
- templateId: this.templateId,
- version: this.version,
- nodeNo: this.nodeNo
- })
- const result = data.data
- ......
- this.userList = result
- .......
- },
-
- }
- }
- </script>
-
- <style lang="less" scoped>
- @import url("./index.less");
- </style>
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
- <template>
- <div class="address-book-single">
- <van-search
- v-model="keywords"
- :placeholder="搜索"
- @search="onSearch"
- @clear="onClear"
- />
- <!-- 人员 -->
- <div v-for="(item, index) in filteredList" :key="item.id">
- {{ item.name }}
- </div>
-
- </div>
- </template>
-
- <script>
- import { Search} from 'vant'
-
- export default {
- ......
- data() {
- return {
- userList: [],
- keywords: '', // 搜索名称
- filterdeList: [],
- }
- },
-
- mounted() {
- this.getUsersList()
- },
-
- methods: {
- async getUsersList() {
- ......
- this.userList = result
- this.filteredList = this.userList
- .......
- },
- //搜索框过滤
- onSearch() {
- this.filteredList = this.userList.filter(item=>
- item.name.toLowerCase().includes(this.keywords.toLowerCase()))
- },
- onClear(){
- this.getUsersList()
- }
-
- }
- }
- </script>
-
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
- async getUsersList() {
- this.loadUser = true
- this.showLoading()
- // let { data } = await mine.approvalsApi.getNodeUsers({
- // templateId: this.templateId,
- // version: this.version,
- // nodeNo: this.nodeNo
- // })
- // const result = data.data
-
- /*模拟多条数据测试filter方法是否正确*/
- const result = [
- {
- "id":"1826653056754913280",
- "name":"小明"
- },
- {
- "id":"1826",
- "name":"pk"
- },
- {
- "id":"182665",
- "name":"PK"
- },
- {
- "id":"18",
- "name":"小明12"
- },
- {
- "id":"18",
- "name":"123"
- },
- ]
- if (result && result.length) {
- this.loadUser = false
- this.hideLoading()
- result.forEach((item) => {
- item.isChecked = this.checkedUser.some((uitem) => item.id === uitem.id)
- })
- if (result.length === this.checkedUser.length) {
- // 是否为全选
- this.checkedUserCount = result.length
- }
- this.userList = result
- } else if (result.length === 0) {
- this.$toast.clear()
- }
- },
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
<van-search>
组件是 Vant(一个轻量、可靠的移动端 Vue 组件库)中的一个搜索组件,在search
、input
和 clear
三个事件
input
事件search
事件<van-search>
组件的配置和具体实现,但大多数情况下,它是在用户明确表示要执行搜索操作时触发的。clear
事件input
事件用于处理用户输入过程中的每一次变化,适合实现即时搜索或搜索建议。search
事件用于处理用户完成输入并明确发起搜索请求的情况,适合执行真正的搜索操作。clear
事件用于处理用户清空搜索框的操作,适合执行与清空操作相关的逻辑。Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。