Vant 4官网例子使用与setup语法糖冲突解决方法

Barrage 弹幕 - Vant 4

例子:弹幕组件的使用

html 复制代码
export default {
  setup() {
    const defaultList = [
      { id: 100, text: '轻量' },
      { id: 101, text: '可定制的' },
      { id: 102, text: '移动端' },
      { id: 103, text: 'Vue' },
      { id: 104, text: '组件库' },
      { id: 105, text: 'VantUI' },
      { id: 106, text: '666' },
    ];

    const list = ref([...defaultList]);
    const add = () => {
      list.value.push({ id: Math.random(), text: 'Barrage' });
    };

    return { list, add };
  },
};

官网的数据处理部分使用export暴露函数写的这与 setup语法糖有冲突。

html 复制代码
<script setup>
</script>

解决方案:

去除外层暴露函数及返回。

html 复制代码
//原数据格式,官网提供。
export default {
  setup() {
    const defaultList = [
      { id: 100, text: '轻量' },
      { id: 101, text: '可定制的' },
      { id: 102, text: '移动端' },
      { id: 103, text: 'Vue' },
      { id: 104, text: '组件库' },
      { id: 105, text: 'VantUI' },
      { id: 106, text: '666' },
    ];

    const list = ref([...defaultList]);
    const add = () => {
      list.value.push({ id: Math.random(), text: 'Barrage' });
    };

    return { list, add };
  },

//处理后的,删除可以使用setup语法糖。
const defaultList = [
      { id: 100, text: '轻量' },
      { id: 101, text: '可定制的' },
      { id: 102, text: '移动端' },
      { id: 103, text: 'Vue' },
      { id: 104, text: '组件库' },
      { id: 105, text: 'VantUI' },
      { id: 106, text: '666' },
    ];

    const list = ref([...defaultList]);
    const add = () => {
      list.value.push({ id: Math.random(), text: 'Barrage' });
    };

去除外层

export default {

setup() {

return { list, add }; }, };

然后引用组件就可以愉快的玩耍了

html 复制代码
<script setup>
const defaultList = [
      { id: 100, text: '轻量' },
      { id: 101, text: '可定制的' },
      { id: 102, text: '移动端' },
      { id: 103, text: 'Vue' },
      { id: 104, text: '组件库' },
      { id: 105, text: 'VantUI' },
      { id: 106, text: '666' },
    ];

    const list = ref([...defaultList]);
    const add = () => {
      list.value.push({ id: Math.random(), text: 'Barrage' });
    };
</script>
<template>
    <van-barrage v-model="list" ref="barrage" :auto-play="false">
  <div class="video" style="width: 100%; height: 150px"></div>
</van-barrage>
<van-space style="margin-top: 10px">
  <van-button @click="add" type="primary" size="small" :disabled="!isPlay">
    弹幕
  </van-button>
  <van-button @click="toggle()" size="small">
    {{ isPlay ? '暂停' : '开始' }}
  </van-button>
</van-space>
</template>

这个是轮播图的处理例子,其他带数据的组件都可以这样处理。

冲突的原因是setup语法糖已经帮我们对数据进行暴露处理了。

相关推荐
资深前端外卖员几秒前
【nodejs高可用】前端APM应用监控方案 + 落地
前端·后端
OhBonsai1 分钟前
Shader 图像处理1_ToneMap技术处理过曝
前端
突头小恐龙1 分钟前
Chrome devTools - Lighthouse
前端·javascript·chrome
谦谦橘子1 分钟前
手写tiny webpack,理解webpack原理
前端·javascript·webpack
土豆12503 分钟前
Tailwind CSS 精通指南:提升效率、可维护性与最佳实践
前端·css
花生了什么树lll3 分钟前
面试中被问到过的前端八股(四)
前端·面试
zqlcoding4 分钟前
使用el-table表格动态渲染表头数据之后,导致设置fixed的列渲染出现问题
前端·javascript·vue.js
海底火旺4 分钟前
破解二维矩阵搜索难题:从暴力到最优的算法之旅
javascript·算法·面试
爱吃的强哥15 分钟前
vue3 使用 vite 管理多个项目,实现各子项目独立运行,独立打包
前端·javascript·vue.js
涵信24 分钟前
第十节:性能优化高频题-虚拟DOM与Diff算法优化
javascript·vue.js·性能优化