由于各类笔试面试,最近都没时间作一些值得分享的东西,正好复习一下vue技术栈,与angular作一下对比。html
angular1就跟vue比略low了。vue
1.数据绑定es6
ng1 ng-bind,{{ scope }} 双向是ng-model面试
ng2 {{scope}} [scope] 双向是[(scope)] (ng2的绑定拆分开来,并且支持函数返回是很棒的设计)redux
vue v-bind , {{ scope }} ,:scope 双向是v-modelapi
2.组件化app
ng1的组件化就是每一个指令堆积起来的,自己并无特别组件化的思想,只是你们把指令分为组件化指令和装饰性指令。那ng1的组件化就是angular注册好多指令dom
ng2的组件化是ts的class,好比:ide
@Component({ selector: 'my-app', template: ` <h1>{{title}}</h1> `, styles: [` .selected { background-color: #CFD8DC !important; color: white; } `] }) export class AppComponent { title = 'Tour of Heroes'; }
ng2的组件用ts的类实现,每一个组件一个文件,而后经过import引入。用上高级语言就是比ng1高大上。函数
vue的组件能够分到文件也能够注册vue全局组件,纯vue的话须要:
var MyComponent = Vue.extend({ template: '<div>A custom component!</div>' }) // 注册 Vue.component('my-component', MyComponent) // 建立根实例 new Vue({ el: '#example' })
固然,加es6的话vue组件就变成了一个个.vue文件:
<style scoped> .container { border: 1px solid #00f; } </style> <template> <div class="container"> <h2 class="red">{{msg}}</h2> <Bpp :ok='msg' ></Bpp> </div> </template> <script> import Bpp from './bpp.vue' export default { data () { return { msg: "456" } }, components: { Bpp } } </script>
我更喜欢这种形式,一个组件的样式,js,dom都在一个文件里,或者在一个文件夹里。固然.vue须要过一下vue-loader。
3.组件通讯
ng1父组件跟子组件通讯,至关于props把属性写到子组件上,还能够公用一个$scope,service,还有万能的事件系统。
反过来共用$scope也是行得通的,service,事件系统。。在传统的组件流中是不推荐子组件与父组件通讯的。
ng2通讯
<my-hero-detail [hero]="selectedHero"></my-hero-detail>
@Component({ selector: 'my-hero-detail', template: ` <div *ngIf="hero"> <h2>{{hero.name}} details!</h2> </div> ` }) export class HeroDetailComponent { @Input() hero: Hero; }
相似props,但须要@Input声明。
vue通讯是经过props字段,
<Bpp :ok='msg' ></Bpp>
在Bpp组件里声明props属性来接受ok:
<script> export default { props:['ok'], data(){ return { data:1 } } } </script>
能够看到ng2和vue的实现方式已经很相似了,只是ng2的ts经过注解来实现的,vue经过属性,api设计趋于大同。。
子组件到父组件的传输方式略有不一样,大致解决思路就是观察者模式,不过也能够用redux这种全局的store思想。
ng2的观察者模式是rxjs,vue的就是自带的事件系统,各有优劣,ng2的rxjs功能强大,写起来简单舒服,可是须要引入rxjs,不过ng2自己就依赖rxjs。
4.路由
ng1路由ng-router,提供了 $routeProvider来控制路由,若是用到权限控制要:
$routeProvider .when('/home', { templateUrl: 'views/home.html', controller: 'HomeCtrl', auth:'home' })
auth是本身加的属性,为实现权限的控制,ng-router还提供了钩子函数: $rootScope.$on('$routeChangeStart', function(scope, next, current) {})。这样就能够判断权限了。
相似的vue:
router.map({ '/a': { component: { ... }, auth: true // 这里 auth 也是一个自定义字段 } })
路由嵌套的话在该路由的对象上面加一个subRoutes。然而ng-router不支持嵌套,就须要第三方库ui-router。
vue这里也提供了钩子函数,为验证auth:
router.beforeEach(function (transition) { if (transition.to.auth) { // 对用户身份进行验证... } })
对于ng2,路由好像还没稳定下来,不知道api会不会改。。:
const appRoutes: Routes = [ { path: 'heroes', component: HeroesComponent } ];
ng2的验证auth为:
{ path: 'guanggao', component: MainContentComponent, canActivate: [BlockIn] }
有个canActivate属性,BlockIn是一个类,咱们能够在BlockIn里写一些权限验证什么的。
三者的路由好像没怎么改,配置api都是相似的。
5.动画
ng1的动画模块,ng2强大的动画系统:
animations: [ trigger('heroState', [ state('inactive', style({ backgroundColor: '#eee', transform: 'scale(1)' })), state('active', style({ backgroundColor: '#cfd8dc', transform: 'scale(1.1)' })), transition('inactive => active', animate('100ms ease-in')), transition('active => inactive', animate('100ms ease-out')) ]) ]
vue也提供了动画钩子:
Vue.transition('expand', { beforeEnter: function (el) { el.textContent = 'beforeEnter' }, enter: function (el) { el.textContent = 'enter' }, afterEnter: function (el) { el.textContent = 'afterEnter' }, enterCancelled: function (el) { // handle cancellation }, beforeLeave: function (el) { el.textContent = 'beforeLeave' }, leave: function (el) { el.textContent = 'leave' }, afterLeave: function (el) { el.textContent = 'afterLeave' }, leaveCancelled: function (el) { // handle cancellation } })
ng1和vue都会给dom添加一些固定class...