programing

Vuex: 정의되지 않은 속성 '$store'를 읽을 수 없습니다.

randomtip 2022. 9. 3. 09:51
반응형

Vuex: 정의되지 않은 속성 '$store'를 읽을 수 없습니다.

vue.js에 스토어가 설정되어 있으며 컴포넌트의 계산된 부분에서 상태 파라미터에 액세스할 수 있습니다.

computed: {
    BASE_URL () {
    return this.$store.state.BASE_URL;  
  }

단, 같은 컴포넌트의 방법으로 스토어에 접속하려고 하면 다음과 같이 됩니다.

  methods: {

    register: function () {
          axios.post( this.BASE_URL + "/web/register", {
          username: this.username,
          password: this.password,
          email: this.email
        }).then(function(data){
          this.$store.commit('saveToken', data.token);
          console.log('token is set to:',  this.$store.state.token)
        });
    }  
 },

콘솔에서 다음 오류가 나타납니다.

미포함(약속)TypeError: 정의되지 않은 속성 '$store'를 읽을 수 없습니다.

나도 해봤어$store없이.this같은 에러가 발생합니다.

여기 뭐가 잘못됐나요?어떻게 하면 고칠 수 있죠?

화살표 기능 대신 Javascript 기능을 사용하고 있습니다.이걸 써보면 효과가 있을 거예요.

 methods: {
    register () {
          axios.post( this.BASE_URL + "/web/register", {
          username: this.username,
          password: this.password,
          email: this.email
        }).then( (data) => {
          this.$store.commit('saveToken', data.token);
          console.log('token is set to:',  this.$store.state.token)
        });
    }  

언급URL : https://stackoverflow.com/questions/46335667/vuex-cannot-read-property-store-of-undefined

반응형