虾仁A 2022-05-23 11:13 采纳率: 78.8%
浏览 83
已结题

为什么在定义了vant的变量还会报错

问题遇到的现象和发生背景
  <van-search
        placeholder="请输入搜索关键词"
        v-model="value"
        shape="round"
        class="search"
      />

<van-swipe :autoplay="3000">
      <van-swipe-item v-for="(image, index) in state" :key="index">
        <img v-lazy="image" />
      </van-swipe-item>
    </van-swipe>
问题相关代码,请勿粘贴截图
<script >
import { onMounted, reactive } from "vue";
import axios from 'axios';
export default {
  setup () {
    const state = reactive({
      images: [
        'https://img.yzcdn.cn/1.jpg',
        'https://img.yzcdn.cn/2.jpg'
      ]
    });
    onMounted(() => {
      axios.get('http://localhost:3000//banner?type=2').then((res) => {
        console.log(res);
        state.images = res.data.banners
      })
    })
    return {
      state,
      value
    };
  },
};
</script>

运行结果及报错内容
vue.runtime.esm.js?c320:619 
        
       [Vue warn]: Property or method "state" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property. See: https://vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties.

found in

---> <Find> at src/views/Find.vue
       <App> at src/App.vue
         <Root>
[Vue warn]: Property or method "value" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property. See: https://vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties.

found in

---> <Find> at src/views/Find.vue
       <App> at src/App.vue
         <Root>
我的解答思路和尝试过的方法
我想要达到的结果
  • 写回答

3条回答 默认 最新

  • Heerey525 前端领域新星创作者 2022-05-23 11:59
    关注

    使用state包裹value和images使用

    <template>
      <div>
        <van-search
          placeholder="请输入搜索关键词"
          v-model="state.value"
          shape="round"
          class="search"
        />
    
        <van-swipe :autoplay="3000">
          <van-swipe-item v-for="(image, index) in state.images" :key="index">
            <img v-lazy="image" />
          </van-swipe-item>
        </van-swipe>
      </div>
    </template>
    <script>
    import { onMounted, reactive } from "vue";
    import axios from "axios";
    export default {
      setup() {
        const state = reactive({
          value: '',
          images: ["https://img.yzcdn.cn/1.jpg", "https://img.yzcdn.cn/2.jpg"],
        });
        onMounted(() => {
          axios.get("http://localhost:3000//banner?type=2").then((res) => {
            console.log(res);
            state.images = res.data.banners;
          });
        });
        return {
          state
        };
      },
    };
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 5月31日
  • 已采纳回答 5月23日
  • 创建了问题 5月23日

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度