我在vue.js中设置了一个商店,可以访问compoenent的计算部分中的状态参数:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
UIBarButtonItem.appearance()
.setTitleTextAttributes([NSFontAttributeName : UIUtils.getFont(14)],
for: UIControlState())
FirebaseApp.configure()
// setupUrbanAirship()
return true
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
// Pass device token to auth
Auth.auth().setAPNSToken(deviceToken, type: AuthAPNSTokenType.sandbox)
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
Auth.auth().canHandleNotification(userInfo)
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any],
fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
if Auth.auth().canHandleNotification(userInfo) {
completionHandler(.noData)
return
}
}
fileprivate func setupUrbanAirship() {
let filename = PropertyUtils.getUAPropertyFileName()
let path = Bundle.main.path(forResource: filename, ofType: "plist")
let config = UAConfig(contentsOfFile: path!)
//config.automaticSetupEnabled = false
UAirship.takeOff(config)
let channelID = UAirship.push().channelID
print("My Application Channel ID: \(channelID)")
UAirship.push().userPushNotificationsEnabled = true
}
但是,当我尝试在同一组件的方法中访问商店时:
computed: {
BASE_URL () {
return this.$store.state.BASE_URL;
}
我在控制台收到此错误:
Uncaught(承诺)TypeError:无法读取属性'$ store'的 未定义
我还尝试了 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)
});
}
},
而没有$store
,但得到了同样的错误。
这里有什么问题?我该如何解决?
答案 0 :(得分:11)
您正在使用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)
});
}