简体   繁体   English

从Vue-Typescript导入状态(由javascript开发)

[英]Import State(developed by javascript) from Vue-Typescript

My project was developed by Vue js and it has store developed by javascript, and I integrated new component which is developed by vue-typescript and all scripts in this components are developed by typescript. 我的项目是由Vue js开发的,并且它的存储库是由javascript开发的,并且我集成了由vue-typescript开发的新组件,并且该组件中的所有脚本都是由typescript开发的。

To import state variable, I tried two ways 要导入状态变量,我尝试了两种方法

use Vuex 使用Vuex

import { mapGetters, mapState, mapActions, mapMutations, createNamespacedHelpers } from 'vuex'

@Component({
  computed: {
    ...mapState([
      'allowedUrls'
    ])
  }
})

this case this.allowedUrls is not defined error 这种情况下this.allowedUrls未定义错误

Use Vuex-Class 使用Vuex-Class

import { State } from 'vuex-class'
@State allowedUrls: string[]

this case, this.allowedUrls is always undefined 在这种情况下, this.allowedUrls始终是未定义的

How can I fix this problem? 我该如何解决这个问题?

Not entirely sure what is wrong with your code but I do the same thing in a slightly different way and it works: 不能完全确定您的代码出了什么问题,但是我以略有不同的方式执行了相同的操作,并且可以正常工作:

import { mapState } from 'vuex';
import RootState from '@/store/rootState';

@Component({
  computed: mapState({
    userFullName: (state: RootState) => state.user.fullName,
    userEmail: (state: RootState) => state.user.email
  })
})

RooState is my class for the vuex state. RooState是我的vuex状态类。 I'm using vue 2.5.17 and vuex 3.0.1. 我正在使用vue 2.5.17和vuex 3.0.1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM