资讯专栏INFORMATION COLUMN

angularjs利用ui-route异步加载组件

lunaticf / 702人阅读

摘要:异步加载各个组件就很有必要。在这里我就以为框架来进行异步加载说明。而为了将服务进行异步加载我们不能用普通的或者。而需要调用或者如果采用进行编译打包的话就需要的支持,这样可以对进行拆分打包,达到异步加载的目的。

ui-route相比于angularjs的原生视图路由更好地支持了路由嵌套,状态转移等等。随着视图不断增加,打包的js体积也会越来越大,比如我在应用里面用到了wangeditor里面多带带依赖的jquery就300多k。异步加载各个组件就很有必要。在这里我就以ui-route为框架来进行异步加载说明。

首先看一下路由加载文件

</>复制代码

  1. angular.module("webtrn-sns").config(["$stateProvider", function ($stateProvider) {
  2. $stateProvider.state({
  3. name: "home.message",
  4. url: "/message",
  5. abstract: true,
  6. templateProvider: ["resources", function (resources) {
  7. return resources.template
  8. }],
  9. controllerProvider: ["resources", (resources)=> {
  10. return resources.controller
  11. }],
  12. onEnter: ["resources", (resources)=>resources.css.use()],
  13. onExit: ["resources", (resources)=>resources.css.unuse()],
  14. resolve: {
  15. resources: ()=> {
  16. return new Promise(
  17. resolve => {
  18. require([], () => {
  19. resolve({
  20. css: require("./css/message_box.css"),
  21. template: require("./html/message_box.html"),
  22. controller: require("./js/message_box.js")
  23. })
  24. })
  25. }
  26. );
  27. }
  28. }
  29. }
  30. ).state({
  31. name: "home.message.add_message",
  32. url: "/add_message?isReply&toUid&title",
  33. params: {isReply: null, toUid: null, title: null},
  34. templateProvider: ["resources", function (resources) {
  35. return resources.template
  36. }],
  37. controllerProvider: ["resources", (resources)=> {
  38. return resources.controller
  39. }],
  40. onEnter: ["resources", (resources)=>resources.css.use()],
  41. onExit: ["resources", (resources)=>resources.css.unuse()],
  42. resolve: {
  43. resources: ()=> {
  44. return new Promise(
  45. resolve => {
  46. require(["./js/message.js"], () => {
  47. resolve({
  48. css: require("./css/add_message.css"),
  49. template: require("./html/add_message.html"),
  50. controller: require("./js/add_message.js")
  51. })
  52. })
  53. }
  54. );
  55. }
  56. }
  57. }
  58. )
  59. }])

这个是路由状态的一个声明文件,name,url,param字段的方式不变,关键是看resolve这个部分。根据ui-route的resolve文档,resolve是为了给state或者controller进行自定义注入对象的。

下面是举出文档中关于resolve的例子:

</>复制代码

  1. $stateProvider.state("myState", {
  2. resolve:{
  3. // Example using function with simple return value.
  4. // Since it"s not a promise, it resolves immediately.
  5. simpleObj: function(){
  6. return {value: "simple!"};
  7. },
  8. // Example using function with returned promise.
  9. // This is the typical use case of resolve.
  10. // You need to inject any services that you are
  11. // using, e.g. $http in this example
  12. promiseObj: function($http){
  13. // $http returns a promise for the url data
  14. return $http({method: "GET", url: "/someUrl"});
  15. },
  16. // Another promise example. If you need to do some
  17. // processing of the result, use .then, and your
  18. // promise is chained in for free. This is another
  19. // typical use case of resolve.
  20. promiseObj2: function($http){
  21. return $http({method: "GET", url: "/someUrl"})
  22. .then (function (data) {
  23. return doSomeStuffFirst(data);
  24. });
  25. },
  26. // Example using a service by name as string.
  27. // This would look for a "translations" service
  28. // within the module and return it.
  29. // Note: The service could return a promise and
  30. // it would work just like the example above
  31. translations: "translations",
  32. // Example showing injection of service into
  33. // resolve function. Service then returns a
  34. // promise. Tip: Inject $stateParams to get
  35. // access to url parameters.
  36. translations2: function(translations, $stateParams){
  37. // Assume that getLang is a service method
  38. // that uses $http to fetch some translations.
  39. // Also assume our url was "/:lang/home".
  40. return translations.getLang($stateParams.lang);
  41. },
  42. // Example showing returning of custom made promise
  43. greeting: function($q, $timeout){
  44. var deferred = $q.defer();
  45. $timeout(function() {
  46. deferred.resolve("Hello!");
  47. }, 1000);
  48. return deferred.promise;
  49. }
  50. },
  51. // The controller waits for every one of the above items to be
  52. // completely resolved before instantiation. For example, the
  53. // controller will not instantiate until promiseObj"s promise has
  54. // been resolved. Then those objects are injected into the controller
  55. // and available for use.
  56. controller: function($scope, simpleObj, promiseObj, promiseObj2, translations, translations2, greeting){
  57. $scope.simple = simpleObj.value;
  58. // You can be sure that promiseObj is ready to use!
  59. $scope.items = promiseObj.data.items;
  60. $scope.items = promiseObj2.items;
  61. $scope.title = translations.getLang("english").title;
  62. $scope.title = translations2.title;
  63. $scope.greeting = greeting;
  64. }
  65. })

我们可以看到resolve的对象是支持Promise的。

再回到我们之前的代码templateProvidercontrollerProvider我们注入了resources的模板对象和controller对象,onEnteronExit注入了css模块。

如果controller中依赖了服务怎么办的?

</>复制代码

  1. resolve: {
  2. resources: ()=> {
  3. return new Promise(
  4. resolve => {
  5. require(["./js/message.js"], () => {
  6. resolve({
  7. css: require("./css/add_message.css"),
  8. template: require("./html/add_message.html"),
  9. controller: require("./js/add_message.js")
  10. })
  11. })
  12. }
  13. );
  14. }
  15. }

可以在require里面将服务注入,如代码中的message.js。而为了将服务进行异步加载我们不能用普通的.factory或者.service。而需要调用$provide.factory或者$provide.service

如果采用webpack进行编译打包的话就需要webpack.optimize.CommonsChunkPlugin的支持,这样可以对js进行拆分打包,达到异步加载js的目的。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/80777.html

相关文章

  • ui-router学习

    摘要:关于应用程序的行为类似于状态机。将应用程序的每个功能视为一组状态。当嵌套状态处于活动状态时,该片段将附加到浏览器中父状态的。父状态可用于限制对整个子状态树的访问,但本身不提供任何。如果方案返回,则转换将暂停,直到解决状态顶级状态嵌套状态 关于state UI-Router应用程序的行为类似于状态机。 将应用程序的每个功能视为一组状态。一次只能有一个状态为活动状态。用户可以从一种状态转换...

    sixleaves 评论0 收藏0
  • 我用ionic框架写了个豆瓣图书馆

    摘要:集移动应用与框架与一身的框架利用我们可以轻易制造出一款带有原生应用与体验的移动这次我们就搭建一个图书还能听歌看电影实现了一些基本的功能,搜索图书类型,查看图书详情,搜索图书标签,查看图书笔记等。前端交互采用了与绑定的。 ionic : 集移动应用UI与 angularjs 框架与一身的 Hybrid App 框架 利用 ionic 我们可以轻易制造出一款带有原生应用UI与体验的移动A...

    eechen 评论0 收藏0
  • angularjs+springMvc学习笔记

    摘要:回调说白了,就是把函数当参数传给另一根函数,在另一个函数执行时调用此函数例如,在下面这段代码中,上面定义了两个函数和,下面的方法请求成功执行,失败执行异步异步的原理我看了网上的一些博客和例子,大都以定时任务为例子说明,但具体的原理我还是不太 回调 说白了,就是把函数当参数传给另一根函数,在另一个函数执行时调用此函数例如,在下面这段代码中,上面定义了两个函数success和error,下...

    dreamGong 评论0 收藏0
  • angularjs+springMvc学习笔记

    摘要:回调说白了,就是把函数当参数传给另一根函数,在另一个函数执行时调用此函数例如,在下面这段代码中,上面定义了两个函数和,下面的方法请求成功执行,失败执行异步异步的原理我看了网上的一些博客和例子,大都以定时任务为例子说明,但具体的原理我还是不太 回调 说白了,就是把函数当参数传给另一根函数,在另一个函数执行时调用此函数例如,在下面这段代码中,上面定义了两个函数success和error,下...

    rozbo 评论0 收藏0
  • 【知识点】为什么推荐用ui-router替代ngRoute

    摘要:被认为是为开发者提供的最实用的一个模块。与集成的服务不同的是,可以将视图嵌套,因为它基于的是操作状态而仅非。与传统做法使用不同的是,在里需要使用服务。当在中处理路由和状态时,开发者的重心是当前的状态是什么以及在哪一个页面里。 初学angularjs,第一个实例是官网的phoneCat,里面路由用的是ngRoute,后来看到别的用ui-router,觉得好奇,ui-route是什么呢?百...

    Shonim 评论0 收藏0

发表评论

0条评论

lunaticf

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<