问题描述: 在Web构建中,使用App-routing有效,但在Cordova构建中无效。
解决方法: 在Cordova构建中,App-routing可能会无效,因为Cordova应用程序是基于WebView的原生应用程序,而不是基于浏览器。这意味着Cordova应用程序无法直接使用浏览器的路由功能。
解决方法之一是使用Ionic提供的Ionic Native Router插件,该插件为Cordova应用程序提供了路由功能。以下是一个示例代码:
npm install @ionic-native/router
import { NativeRouter } from '@ionic-native/router/ngx';
@NgModule({
...
providers: [
NativeRouter
],
...
})
export class AppModule { }
import { NativeRouter } from '@ionic-native/router/ngx';
@Component({
selector: 'app-root',
templateUrl: 'app.component.html',
styleUrls: ['app.component.scss']
})
export class AppComponent {
constructor(private nativeRouter: NativeRouter) { }
ngOnInit() {
this.nativeRouter.configure({
'/home': { component: HomePage },
'/about': { component: AboutPage },
// Add more routes here
});
}
}
Home
About
通过使用Ionic Native Router插件,可以在Cordova构建中有效地实现路由功能。这样,您的应用程序将在Web和Cordova构建中都能正常工作。