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语法糖已经帮我们对数据进行暴露处理了。

相关推荐
Sailing11 分钟前
🚀AI 写代码越来越快,但我开始不敢上线了
前端·后端·面试
Alair‎12 分钟前
Vue 3 基础教程:从入门到精通
前端·javascript·vue.js
百锦再13 分钟前
树形数据展示:树形表格与树形控件的深度对比(Vue实现)
javascript·vue.js·ecmascript·递归·tree·data·table
Sylvia33.14 分钟前
体育数据API实战:用火星数据实现NBA赛事实时比分与状态同步
java·linux·开发语言·前端·python
码农阿豪15 分钟前
Vue+Ant Design表格组件开发实战:从问题到优化的完整指南
前端·javascript·vue.js
QQ243919716 分钟前
spring boot医院挂号就诊系统信息管理系统源码-SpringBoot后端+Vue前端+MySQL【可直接运行】
前端·vue.js·spring boot
Coder-coco17 分钟前
家政服务管理系统|基于springboot + vue家政服务管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·家政服务管理系统
用户693717500138418 分钟前
OS级AI Agent:手机操作系统的下一个战场
android·前端·人工智能
百锦再18 分钟前
Vue不是万能的:前后端不分离开发的优势
前端·javascript·vue.js·前端框架·vue
毕设源码_王学姐19 分钟前
2026毕设ssm+vue民宿管理系统论文+程序
前端·vue.js·课程设计