摘要:异步加载各个组件就很有必要。在这里我就以为框架来进行异步加载说明。而为了将服务进行异步加载我们不能用普通的或者。而需要调用或者如果采用进行编译打包的话就需要的支持,这样可以对进行拆分打包,达到异步加载的目的。
ui-route相比于angularjs的原生视图路由更好地支持了路由嵌套,状态转移等等。随着视图不断增加,打包的js体积也会越来越大,比如我在应用里面用到了wangeditor里面多带带依赖的jquery就300多k。异步加载各个组件就很有必要。在这里我就以ui-route为框架来进行异步加载说明。
首先看一下路由加载文件
</>复制代码
angular.module("webtrn-sns").config(["$stateProvider", function ($stateProvider) {
$stateProvider.state({
name: "home.message",
url: "/message",
abstract: true,
templateProvider: ["resources", function (resources) {
return resources.template
}],
controllerProvider: ["resources", (resources)=> {
return resources.controller
}],
onEnter: ["resources", (resources)=>resources.css.use()],
onExit: ["resources", (resources)=>resources.css.unuse()],
resolve: {
resources: ()=> {
return new Promise(
resolve => {
require([], () => {
resolve({
css: require("./css/message_box.css"),
template: require("./html/message_box.html"),
controller: require("./js/message_box.js")
})
})
}
);
}
}
}
).state({
name: "home.message.add_message",
url: "/add_message?isReply&toUid&title",
params: {isReply: null, toUid: null, title: null},
templateProvider: ["resources", function (resources) {
return resources.template
}],
controllerProvider: ["resources", (resources)=> {
return resources.controller
}],
onEnter: ["resources", (resources)=>resources.css.use()],
onExit: ["resources", (resources)=>resources.css.unuse()],
resolve: {
resources: ()=> {
return new Promise(
resolve => {
require(["./js/message.js"], () => {
resolve({
css: require("./css/add_message.css"),
template: require("./html/add_message.html"),
controller: require("./js/add_message.js")
})
})
}
);
}
}
}
)
}])
这个是路由状态的一个声明文件,name,url,param字段的方式不变,关键是看resolve这个部分。根据ui-route的resolve文档,resolve是为了给state或者controller进行自定义注入对象的。
下面是举出文档中关于resolve的例子:
</>复制代码
$stateProvider.state("myState", {
resolve:{
// Example using function with simple return value.
// Since it"s not a promise, it resolves immediately.
simpleObj: function(){
return {value: "simple!"};
},
// Example using function with returned promise.
// This is the typical use case of resolve.
// You need to inject any services that you are
// using, e.g. $http in this example
promiseObj: function($http){
// $http returns a promise for the url data
return $http({method: "GET", url: "/someUrl"});
},
// Another promise example. If you need to do some
// processing of the result, use .then, and your
// promise is chained in for free. This is another
// typical use case of resolve.
promiseObj2: function($http){
return $http({method: "GET", url: "/someUrl"})
.then (function (data) {
return doSomeStuffFirst(data);
});
},
// Example using a service by name as string.
// This would look for a "translations" service
// within the module and return it.
// Note: The service could return a promise and
// it would work just like the example above
translations: "translations",
// Example showing injection of service into
// resolve function. Service then returns a
// promise. Tip: Inject $stateParams to get
// access to url parameters.
translations2: function(translations, $stateParams){
// Assume that getLang is a service method
// that uses $http to fetch some translations.
// Also assume our url was "/:lang/home".
return translations.getLang($stateParams.lang);
},
// Example showing returning of custom made promise
greeting: function($q, $timeout){
var deferred = $q.defer();
$timeout(function() {
deferred.resolve("Hello!");
}, 1000);
return deferred.promise;
}
},
// The controller waits for every one of the above items to be
// completely resolved before instantiation. For example, the
// controller will not instantiate until promiseObj"s promise has
// been resolved. Then those objects are injected into the controller
// and available for use.
controller: function($scope, simpleObj, promiseObj, promiseObj2, translations, translations2, greeting){
$scope.simple = simpleObj.value;
// You can be sure that promiseObj is ready to use!
$scope.items = promiseObj.data.items;
$scope.items = promiseObj2.items;
$scope.title = translations.getLang("english").title;
$scope.title = translations2.title;
$scope.greeting = greeting;
}
})
我们可以看到resolve的对象是支持Promise的。
再回到我们之前的代码templateProvider和controllerProvider我们注入了resources的模板对象和controller对象,onEnter和onExit注入了css模块。
如果controller中依赖了服务怎么办的?
</>复制代码
resolve: {
resources: ()=> {
return new Promise(
resolve => {
require(["./js/message.js"], () => {
resolve({
css: require("./css/add_message.css"),
template: require("./html/add_message.html"),
controller: require("./js/add_message.js")
})
})
}
);
}
}
可以在require里面将服务注入,如代码中的message.js。而为了将服务进行异步加载我们不能用普通的.factory或者.service。而需要调用$provide.factory或者$provide.service
如果采用webpack进行编译打包的话就需要webpack.optimize.CommonsChunkPlugin的支持,这样可以对js进行拆分打包,达到异步加载js的目的。
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/80777.html
摘要:关于应用程序的行为类似于状态机。将应用程序的每个功能视为一组状态。当嵌套状态处于活动状态时,该片段将附加到浏览器中父状态的。父状态可用于限制对整个子状态树的访问,但本身不提供任何。如果方案返回,则转换将暂停,直到解决状态顶级状态嵌套状态 关于state UI-Router应用程序的行为类似于状态机。 将应用程序的每个功能视为一组状态。一次只能有一个状态为活动状态。用户可以从一种状态转换...
摘要:集移动应用与框架与一身的框架利用我们可以轻易制造出一款带有原生应用与体验的移动这次我们就搭建一个图书还能听歌看电影实现了一些基本的功能,搜索图书类型,查看图书详情,搜索图书标签,查看图书笔记等。前端交互采用了与绑定的。 ionic : 集移动应用UI与 angularjs 框架与一身的 Hybrid App 框架 利用 ionic 我们可以轻易制造出一款带有原生应用UI与体验的移动A...
摘要:回调说白了,就是把函数当参数传给另一根函数,在另一个函数执行时调用此函数例如,在下面这段代码中,上面定义了两个函数和,下面的方法请求成功执行,失败执行异步异步的原理我看了网上的一些博客和例子,大都以定时任务为例子说明,但具体的原理我还是不太 回调 说白了,就是把函数当参数传给另一根函数,在另一个函数执行时调用此函数例如,在下面这段代码中,上面定义了两个函数success和error,下...
摘要:回调说白了,就是把函数当参数传给另一根函数,在另一个函数执行时调用此函数例如,在下面这段代码中,上面定义了两个函数和,下面的方法请求成功执行,失败执行异步异步的原理我看了网上的一些博客和例子,大都以定时任务为例子说明,但具体的原理我还是不太 回调 说白了,就是把函数当参数传给另一根函数,在另一个函数执行时调用此函数例如,在下面这段代码中,上面定义了两个函数success和error,下...
摘要:被认为是为开发者提供的最实用的一个模块。与集成的服务不同的是,可以将视图嵌套,因为它基于的是操作状态而仅非。与传统做法使用不同的是,在里需要使用服务。当在中处理路由和状态时,开发者的重心是当前的状态是什么以及在哪一个页面里。 初学angularjs,第一个实例是官网的phoneCat,里面路由用的是ngRoute,后来看到别的用ui-router,觉得好奇,ui-route是什么呢?百...
阅读 3825·2021-11-24 09:39
阅读 2725·2019-08-30 15:54
阅读 1268·2019-08-30 13:01
阅读 3563·2019-08-28 18:30
阅读 1728·2019-08-26 17:44
阅读 3688·2019-08-26 11:31
阅读 2539·2019-08-26 10:40
阅读 1475·2019-08-26 10:27