虽然,现在越来越多的人选择使用react、vue以及ng2,但是依然存在相当一部分人在使用angular1.x开发。本文将介绍如何使用webpack+es6+angular1.x+$oclazyLoad实现动态加载。
1.webpack
webpack.config.js
var path = require('path'); var webpack = require('webpack'); var ExtractTextPlugin = require("extract-text-webpack-plugin"); module.exports = { entry: { home: [ 'babel-polyfill', './app/app.js' //引入文件 ], common: [ 'babel-polyfill', 'angular', 'angular-ui-router', 'oclazyload' ] }, output: { path: path.join(__dirname, '/wap'), filename: '[name].js', chunkFilename: '[id].build.js"htmlcode">import angular from 'angular'; import uirouter from 'angular-ui-router'; import ocLazyLoad from 'oclazyLoad'; angular.module('app',[ uirouter,ocLazyLoad ])模块与模块之间引用
我们建立第二个模块 header/index.js
import angularfrom 'angular'; export default angular.module('header',[]).name修改app.js
import header from './header' angular.module('app',[ uirouter,ocLazyLoad , header])模块的引用完成
3.控制器
假设在header目录下新增一个控制器
header/controller.js
export default class HeaderController { consturctor(){ this.home = 'header' } }引用控制器 修改 header/index.js
import HeaderController from './controller' export default angular.module('header',[ ]) .controller('HeaderController',HeaderController) .name4.服务
如果要在控制器内使用$scope,或者其他服务肯定是报错的,那是因为我们在使用之前没有注入服务
所以第一步应该注入服务
header/controller.js
export default class HeaderController { consturctor($scope){ this.home = 'header' $scope.component = 'head' } } HeaderController.$inject = ['$scope']那么如何自定义服务呢?
新建 header.server.js
class HeaderServices { constructor(){ this.name = 'cxh' } getName(){ return this.name } }header/index.js
import HeaderService from './service'; export default angular.module('header',[ ]) .controller('HeaderController',HeaderController) .service('HeaderService',HeaderService) .name在控制器中使用自定义服务
header/controller.js
export default class HeaderController { consturctor($scope,HeaderService){ this.home = 'header' $scope.component = 'head' $scope.name = HeaderService.getName } } HeaderController.$inject = ['$scope','HeaderService']5.指令
新建 footer/index.js 大致和 header/index.js相同
将 footer模块 引入到 app.js
新建footer/directive.js
export default class Footer { constructor(){ this.restrict = 'E', this.scope = {}, this.template = "<div ng-click='alert()'>footer</div>" this.controller = Foot this.link = (scope, element, attr) => {} } } class Foot{ constructor(){ $scope.alert = () => { alert(1) } } } Header.$inject = ['$scope']footer/index.js
export default angular.module('footer',[]) .directive('footerDirective',()=> new Footer) .name6.路由
router.js
export default router ($stateProvider, $urlRouterProvider) { $stateProvider.state("home",{ url:"/home", templateUrl:'app/home/home.html', controller: "HomeController", controllerAs:"HMC", }) $urlRouterProvider.otherwise('/home'); } router.$inject = [ '$stateProvider', '$urlRouterProvider']DEMO 实现动态加载
1.第一步,新建app.js创建一个module
import angular from 'angular'; import uirouter from 'angular-ui-router'; import ocLazyLoad from 'oclazyLoad'; //引用创建头部 组件 import Header from './header'; //路由 import routing from "./router.js"; //引入两个子模块 import Home from "./homes"; import Router from "./router"; angular.module('app',[uirouter, Header, ocLazyLoad, Home, Router]) .config(routing)2.配置路由 ./route.js
export default function routing($stateProvider, $urlRouterProvider) { 'ngInject'; //注入服务 就不需要使用$inject了 $stateProvider .state("home",{ url:"/home", templateProvider: ($q) => { //动态引入html模板 'ngInject'; let deferred = $q.defer(); require.ensure([], function () { let template = require('./home/home.html'); deferred.resolve(template); }); return deferred.promise; }, controller: "HomeController", controllerAs:"HMC", resolve: { //动态加载模块 loadMyCtrl: function ($q, $ocLazyLoad) { 'ngInject'; let deferred = $q.defer(); require.ensure([], () => { let module = require("./home").default; $ocLazyLoad.load({name: module.name}); deferred.resolve(module.controller) }); return deferred.promise; } } }), .state("route",{ url:"/route", templateProvider: ($q) => { //动态引入html模板 'ngInject'; let deferred = $q.defer(); require.ensure([], function () { let template = require('./router/router.html'); deferred.resolve(template); }); return deferred.promise; }, controller: "routerController", controllerAs:"RTC", resolve: { //动态加载模块 loadMyCtrl: function ($q, $ocLazyLoad) { 'ngInject'; let deferred = $q.defer(); require.ensure([], () => { let module = require("./router").default; $ocLazyLoad.load({name: module.name}); deferred.resolve(module.controller) }); return deferred.promise; } } }) $urlRouterProvider.otherwise('/home'); }3.header
header/index.js
import angular from "angular"; import header from './directive'; export default angular.module('app_header',[]) .directive('header', () => new header) .name;header/directive.js
class Header { constructor($scope){ 'ngInject'; $scope.isshow = false; } } export default class header { constructor() { this.restrict = 'E', this.scope = {}, this.template = require(./header.html) this.controller = Header this.link = (scope, element, attr) => {} } }header/header.html
<div> <a href="#home" rel="external nofollow" >home</a> <a href="#router" rel="external nofollow" >router</a> </div>4.home
home/index.js
import angular from "angular"; import HomeController from './controller'; export default angular.module('app_home',[]) .controller('HomeController', HomeController)home/controller.js
export default class HomeController { constructor($scope) { 'ngInject'; this.isshow = false; this.eage = 'sds'; $scope.edg = 'sma' } change(){ this.isshow = !this.isshow; console.log(this.isshow); } }home/home.html
<div>home {{HMC.eage}} -- {{edg}}</div>其余的模块大同小异就不依依去写了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。