Angular 2 app base href with Cordova IOS

2020-07-09 11:10发布

问题:

I am developing an application using Angular 2 with cordova. I have used the angular cli to build the appplication. I have now ran into some routing issues on startup when it comes to the IOS application. The base href seems to be set wrong. If I set the base href using: <base href="/" target="_blank"> the application doesn't load. If the base href is set when I bootstrap the application using: ...,provide(APP_BASE_HREF, {useValue:'/'}), ... the application will load but I get the following exception:

EXCEPTION: Error: Uncaught (in promise): Error: Cannot match any routes: 'var/containers/Bundle/Application/3C8966ED-7DDD-4309-8C18-10B778C5AE15/test.app/www'

So the application can't function properly since there are critical files that don't get loaded on the start of the application. For android I encountered the same problem but I found the solution, to set the base href in the following question android solution. Have anybody encountered this problem and have a solution?

Edit: I solved the problem by using this: <base href="./" target="_blank">in index html and remove the provide(APP_BASE_HREF, {useValue:'/'}). This way I have to set change the base href depending if I'm deploying the app to android or ios.

回答1:

I set it as

<base href="www">

and all css, js and images file loading are correct.