Uncaught TypeError: this.transitionTo is not a function in route
Uncaught TypeError: this.transitionTo is not a function in route
我试图在成功删除该项目后将我的页面重定向到主页,但它告诉我 transitionTo 不是一个函数。
这是我的代码:
import Route from '@ember/routing/route';
import Ember from 'ember';
import { inject as service } from '@ember/service';
/**
* @module lebtivity/routes/event
*/
export default Route.extend({
/**
* @type {Service}
*/
api: service(),
ajax: Ember.inject.service(),
/**
* @param {Object} params The parameters extracted from the URL.
* @return {Promise<Object>}
*/
model(params) {
return this.get('api').request(`/events/${params.slug}`);
},
actions:{
remove: function (model) {
console.log(model.id);
Ember.$.ajax({
method: "DELETE",
url: model.id,
success: function(data)
{
this.transitionTo('index');
}
})
}
}
});
您的问题可能是 this
不是您想的那样。变量 this
的范围可能在 promise success.
内
您可以做的检查是在调用 DELETE
之前定义另一个变量
let that = this;
并使用
that.transitionTo('index')
使用箭头函数。像这样编写您的操作和 'success' 函数:
actions: {
remove() {
$.ajax({
...
success: () => {
this.transitionTo('index')
}
});
}
}
我试图在成功删除该项目后将我的页面重定向到主页,但它告诉我 transitionTo 不是一个函数。
这是我的代码:
import Route from '@ember/routing/route';
import Ember from 'ember';
import { inject as service } from '@ember/service';
/**
* @module lebtivity/routes/event
*/
export default Route.extend({
/**
* @type {Service}
*/
api: service(),
ajax: Ember.inject.service(),
/**
* @param {Object} params The parameters extracted from the URL.
* @return {Promise<Object>}
*/
model(params) {
return this.get('api').request(`/events/${params.slug}`);
},
actions:{
remove: function (model) {
console.log(model.id);
Ember.$.ajax({
method: "DELETE",
url: model.id,
success: function(data)
{
this.transitionTo('index');
}
})
}
}
});
您的问题可能是 this
不是您想的那样。变量 this
的范围可能在 promise success.
您可以做的检查是在调用 DELETE
之前定义另一个变量let that = this;
并使用
that.transitionTo('index')
使用箭头函数。像这样编写您的操作和 'success' 函数:
actions: {
remove() {
$.ajax({
...
success: () => {
this.transitionTo('index')
}
});
}
}