반응형
TypeError: 정의되지 않은 Vuejs의 속성 'posts'를 설정할 수 없음
나는 VueJs와 Laravel로 SPA를 만든다.홈페이지에서 api laravel과 acxio response를 통해 모든 게시물을 얻는데 데이터 오브젝트가 있었다.그러나 나는 게시물을 업데이트 할 수 없다.
- 크롬 디버그 도구 오류:
웰컴에 있는 내 암호.부에를 하다
import { mapGetters } from 'vuex'
import axios from 'axios'
export default {
name: 'welcome',
layout: 'default',
metaInfo: { titleTemplate: 'Welcome | %s' },
computed: mapGetters({
authenticated: 'authCheck'
}),
data: () => ({
title: 'Demo Blog',
}),
props: {
posts: {
type: Object
}
},
created () {
axios.get('/api/posts')
.then(function (response) {
this.posts = response.data;
})
.catch(function (error) {
console.log(error);
});
},
}
당신은 콜백으로 정규 함수를 사용하고 있다.this
참조 변경여기서는 화살표 기능을 사용해야 한다.() => {}
.
axios.get('/api/posts')
.then((response) => {
this.posts = response.data;
})
.catch((error) => {
console.log(error);
});
우선 당신이 정의한 것posts
당신의 소품 소유지에.어린이 구성품에서 소품을 변이해서는 안 된다.소품은 단방향-데이터 흐름
초기화할 수 있음posts
다음과 같은 데이터 속성:
data(){
return{
posts: null
}
}
그런 다음 API를 통해 데이터를 가져와 할당하면 된다.posts
자료의 속성에 있어서.
this
그대 속에then
함수는 vue 인스턴스를 가리키지 않는다.그래서 이렇게 하는 게 더 낫겠다.
created () {
var vm = this;
axios.get('/api/posts')
.then(function (response) {
vm.posts = response.data;
})
.catch(function (error) {
console.log(error);
});
},
}
아니면 그대 an => 이런 함수
created () {
axios.get('/api/posts')
.then( (response) => {
this.posts = response.data;
})
.catch(function (error) {
console.log(error);
});
},
}
참조URL: https://stackoverflow.com/questions/44342112/typeerror-cannot-set-property-posts-of-undefined-vuejs
반응형
'Programing' 카테고리의 다른 글
Axios의 Cors OPTION 메서드가 Laravel 및 Nginx와 함께 실패함 (0) | 2022.04.29 |
---|---|
GDB가 어떤 주소에 문제가 발생했는지 알려주려면 어떻게 해야 할까? (0) | 2022.04.29 |
Vue JS - 하나의 구성 요소에 조건부로 정보를 표시 (0) | 2022.04.28 |
어떻게 하면 한 끈이 다른 끈을 교체하는 것으로 끝나지 않는 방식으로 두 줄을 교체할 수 있을까? (0) | 2022.04.28 |
C에 대한 설계 원리, 모범 사례 및 설계 패턴(또는 일반적으로 절차 프로그래밍)? (0) | 2022.04.28 |