2017-11-25 116 views
1

我是新來的燼,我試圖用transitionTo與queryParams,但我無法讓它工作我試了很多解決方案,但我找不到什麼是我的代碼錯了。這裏的兩條路線我轉型之間的代碼:Ember TransitionTo與queryParams不起作用

1 index.js:

export default Ember.Route.extend({ 
setupController: function(controller, model) { 
this._super(controller, model); 
controller.set("model", model); 
}, 
model() { 
    return { 
    searchQuery: "" 
    }; 
}, 
actions: { 
    search() { 
    const query = this.controller.get("model.searchQuery"); 
    this.transitionTo("/search-results", { 
    quaryParams: { 
     q: query 
    } 
    }); 
    } 
} 
}); 

2搜索results.js:

export default Ember.Route.extend({ 
model() { 
    return { 
    fieldsInput: [] 
    }; 
}, 
setupController: function(controller, model) { 
    this._super(controller, model); 
    controller.set("model", model); 
} 
}); 

我不知道是否應該添加任何其他內容。我試圖設置控制器上的queryParams,但它也沒有工作。此外,我嘗試添加

queryParams: { 
    q: "" 
    }, 

到搜索結果路由但沒有用。

回答

1

當您爲transitionTo方法提供url時,您需要通過構建包括queryParams來提供完整的URL。這將起作用

this.transitionTo(`/search-results?q=${query}`); 

正如您在評論中提到的,您缺少在路由中指定queryParams屬性。

+0

謝謝你的回覆。但我試過這個,但它也沒有工作。它只是去沒有查詢參數的路線 – moamenezzat

+1

可能有很多不工作的原因。但我在答覆中提到了一個關鍵部分。這是真的,它會起作用。看看這個[twiddle](https://ember-twiddle.com/4b358521437ddbf62be17af55e7b27ce?openFiles=routes.application.js%2C&route=%2Fsearch-results%3Fq%3DchangedValue)。如果你準備不工作旋轉,那麼將很容易解決 – kumkanillam

+0

謝謝你:)。旋律給了我失蹤的一塊。它正在添加路由上的queryParms對象。請添加更新您的答案,將其標記爲正確的答案。 – moamenezzat

0

在「search-results.js」中,您需要訪問(告訴您的模型)關於您從index.js傳入文件的參數,因此在search-results.js中,您的模型應該看起來有點像這樣的:

model(param) { 
    return { 
    fieldsInput: [] 
    }; 
}, 

如果你看看你已經定義的「fieldsInput」上面,你會希望做一些灰燼,數據查找(的,如果你不熟悉它,然後看youtube video or two,它會幫助我們進一步發展),以利用從index.js傳遞過來的「queryParam」。

(提示:「return this.store.findRecord(someObject, param.someProperty)」可能是你想要使用的工具)

另外,如果你在該行看不到,你已經寫了「this.transitionTo」,你在你的錯字「queryParams」的拼寫。

希望這會有所幫助。

+0

非常感謝你的回答。但@ kumkanillam twiddle給了我失蹤的一塊。 – moamenezzat

相關問題