当前位置:Gxlcms > JavaScript > 详解angularjs利用ui-route异步加载组件

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

时间:2021-07-01 10:21:17 帮助过:2人阅读

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的。

再回到我们之前的代码templateProvider和controllerProvider我们注入了resources的模板对象和controller对象,onEnter和onExit注入了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的目的。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

人气教程排行