<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>
使用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>
你现在遍历的是一个对象?
而不是 state.images ?
const state = reactive([
'https://img.yzcdn.cn/1.jpg',
'https://img.yzcdn.cn/2.jpg'
])