我正在尝试导航到某些用户属性匹配的“ BadConfiguration”页面。 但是我一直无法读取未定义错误的属性“ setHash”。 我尝试进行一些更改,然后出现replaceHash错误。它只是一个页面应用程序,如果用户不是一个特定的应用程序-我必须显示错误的“配置”页面。
编辑:已更改为正确反映路由器名称。但是问题是-仅当我将该呼叫导航到其他地方时,它才起作用。例如,如果我在页面上给出一个按钮,则单击该按钮时我会调用其中的一个函数,而我会调用该函数进行导航。 如果我不这样做,并尝试给出该行代码以在init()或initializeView()中导航,则会返回setHash错误。
Manifest.json
"routes": [
{
"pattern": "",
"name": "G_Table",
"target": "G_Table"
},
{
"name": "BadConfiguration",
"pattern": "BadConfiguration",
"greedy": false,
"target": "BadConfiguration"
}],
"targets": {
"G_Table": {
"viewType": "XML",
"transition": "show",
"clearAggregation": true,
"viewName": "G_Table",
"viewLevel": 1,
"controlAggregation": "pages",
"controlId": "app"
},
"BadConfiguration": {
"viewType": "XML",
"transition": "show",
"clearAggregation": true,
"viewName": "BadConfiguration",
"viewLevel": 1,
"controlAggregation": "pages",
"controlId": "app"
}
}
}
}
Component.js
init: function() {
// call the base component's init function
UIComponent.prototype.init.apply(this, arguments);
// set the device model
this.setModel(models.createDeviceModel(), "device");
this.getRouter().initialize();
}
有2个视图,即-> G_Table.xml和BadConfiguration.xml
G_Table.xml
<mvc:View controllerName="Plant_Data_Plant_Data.controller.G_Table"
xmlns:html="http://www.w3.org/1999/xhtml" xmlns:mvc="sap.ui.core.mvc"
xmlns:core="sap.ui.core" displayBlock="true" xmlns="sap.m">
<App id="app">
<pages>
<Page title="Plants" showHeader="true">
G_Table.controller.js
return Controller.extend("Plant_Data_Plant_Data.controller.G_Table", {
onInit: function() {
var userModel = new sap.ui.model.json.JSONModel("/services/userapi/currentUser");
this.getView().setModel(userModel, "userapi");
// here the aim is if some properties satisfy, then route to bad
configuration
if(true)
{
this.getOwnerComponent().getRouter().navTo("bc", {}, false);
//I get setHash undefined error here
}
this.initializeView();
},
initializeView: function() {
this.populateTable();
},
答案 0 :(得分:1)
尝试导航到页面时,需要使用路由器名称。这里的问题是,您正在尝试导航到提供其目标名称的页面。
您必须尝试
this.getOwnerComponent()。getRouter()。navTo(“ BadConfiguration” .....);
有关路由如何工作的示例:Routing and Navigation in SAPUI5 另外,如果您知道何时调用此视图,我认为在这种情况下您不需要该模式。
让我知道是否有帮助。
答案 1 :(得分:0)
请勿在onInit内进行导航。
应该使用onInit事件来初始化有关其控制器的属性。在触发onInit的时刻,其他视图和其他控制器尚未“了解” SAPUI5。
在onInit事件上,注册一个routeMatched事件,然后在routeMatched上进行导航。
赞:
sap.ui.define(['sap/ui/core/mvc/Controller'],
function(Controller) {
"use strict";
var that;
return Controller.extend("sapui5Example.home", {
onInit: function () {
that = this;
sap.ui.core.UIComponent.getRouterFor(that).getRoute('rtHome').attachPatternMatched(
onRouteOrSubRoutesMatched);
}
});
function onRouteOrSubRoutesMatched() {
sap.ui.core.UIComponent.getRouterFor(that).navTo('yourOtherRoute');
}
});
这是个小矮人: https://embed.plnkr.co/YjGzWe/