
这样router一多最大的缺点就是会导致router命名冲突。
拆分!拆分!拆分!
首先考虑API配置文件怎么拆分,对于接口,我们肯定有多套环境,多套环境那么API的URL也不一样,拆分成多个文件后多个文件需要共用同一个获取
apiBase的方法,所以这个
apiBase就要写在公共的地方,在这里原来的
api.config.js就变成了公共配置,
apiBase就放在此文件内。
export function apiBase() {
let hostname = window.location.hostname,
API_BASE_URL = 'http://test2api.dunizb.com';//默认环境
if(hostname === 'crm.dunizb.cn') { //正式环境
API_BASE_URL = 'http://api.dunizb.cn';
} else if(hostname === 'admin.dunizb.com') {//公网测试环境
API_BASE_URL = 'http://testapi.dunizb.com';
} else if(hostname === 'manager.dunizb.com') {//内网测试环境
API_BASE_URL = 'http://test2api.dunizb.com';
}
return API_BASE_URL;
}然后在每个子API配置文件中引入即可:
import {apiBase} from '../api.config';具体功能API不需要更改,直接拷贝相应模块API到子模块API配置文件即可。

Router的拆分稍微复杂一点,拆分后的文件目录与API的目录相同:

拆分思路也完全一样,但要保证只有一个
router.start即:
return router.start(App, '#app');虽然你在子router配置文件中也写上页面是能正常工作的,但是Vue.js会在控制台报一个错误:

这个错误的意思就是router已经启动,无需启动多次。所以,子router文件中不能存在
return router.start(App, '#app'); 这样的代码。拆分后
router.config.js内容如下:
/**
* 路由总文件
* Created by Bing on 2017/6/19 0019.
*/
import App from './App';
import authority from './routers/authority';
import publics from './routers/public';










