胡敏 @新蛋科技
2015
2016
2017
Vue
Angular
React
没有最好的,只有最合适的!强扭的瓜不甜!
// index.js content
import { COMPONENTS } from './components';
const install = Vue => {
if (install.installed) return;
install.installed = true;
COMPONENTS.forEach(comp => {
Vue.component(comp.name, comp);
});
};
// Auto install
if (typeof window !== 'undefined' && window.Vue) {
install(window.Vue);
};
export default {
install,
version: '0.0.1'
};
import App from './app/App';
// import Home from './home/Home';
// import About from './about/About';
// import Layout from './layout/Layout';
// import NotFound from './notfound/NotFound';
const Layout = () => import('./layout/Layout');
const Home = () => import('./home/Home');
const About = () => import('./about/About');
const NotFound = () => import('./notfound/NotFound');
export {
App,
Home,
About,
NotFound,
Layout
};
// modules/product/pages/index.js
const Page21 = () => import(/* webpackChunkName: "product" */'./page21/Page21');
const Page22 = () => import(/* webpackChunkName: "product" */'./page22/Page22');
const Layout = () => import(/* webpackChunkName: "product" */'./layout/Layout');
export {
Layout,
Page21,
Page22
};
// modules/product/index.js
import * as pages from './pages';
export default {
path: 'product', component: pages.Layout, children: [
{ path: 'page21', component: pages.Page21 },
{ path: 'page22', component: pages.Page22 }
]
};
// modules/index.js
import userRotue from './user';
import productRoute from './product';
export const moduleRoutes = [
userRotue,
productRoute
];
import Vue from 'vue';
import VueRouter from 'vue-router';
Vue.use(VueRouter);
import * as pages from './pages';
import { moduleRoutes } from './modules';
const routes = [
{
path: '', component: pages.Layout, children: [
{ path: '', component: pages.Home },
{ path: 'about', component: pages.About },
...moduleRoutes
]
},
{ path: '*', component: pages.NotFound }
];
export const router = new VueRouter({
routes
});
// app.js
const basePath = 'src';
angular.module('app', ['ngRoute'])
.config(['$routeProvider', '$locationProvider', ($routeProvider, $locationProvider) => {
$locationProvider.html5Mode(true);
$routeProvider
.when('/', {
controller: 'HomeCtrl',
controllerAs: 'vm',
templateUrl: `${basePath}/pages/home/home.html`
})
.when('/about', {
controller: 'AboutCtrl',
controllerAs: 'vm',
templateUrl: `${basePath}/pages/about/about.html`
})
}]);
// home.controller.js
(() => {
class HomeCtrl {
constructor() {
this.hello = 'I\'m Home Page';
}
}
angular.module('app')
.controller('HomeCtrl', HomeCtrl);
})();
// index.js
import './src/app';
import './src/pages/home/home.controller';
import './src/pages/about/about.controller';
import './src/bootstrap';
// webpack.config.js
const path = require('path');
module.exports = {
entry: {
app: './index.js'
},
output: {
path: path.join(__dirname, './dist'),
filename: 'js/[name].js'
},
module: {
rules: [{
test: /.js$/,
loader: 'babel-loader'
}]
}
};
Show a random string:
{{user.rnd}}
<script>
export default {
data() {
return {
user: {}
};
},
methods: {
setUserName() {
this.user.rnd = Math.random().toString(16);
}
}
};
</script>
var user = {};
user.name = 'XXX';
function defineReactive(obj, key, value) {
var property = Object.getOwnPropertyDescriptor(obj, key);
var getter = property && property.get;
var setter = property && property.set;
Object.defineProperty(obj, key, {
configurable: true,
enumerable: true,
get() {
return getter ? getter.call(obj) : value;
},
set(newVal) {
// 先取出当前值
var val = getter ? getter.call(obj) : value;
// 值一样,直接return
if (newVal === val || (newVal !== newVal && val !== val)) {
return;
}
// 把值设置回去
if (setter) {
setter.call(obj, newVal);
} else {
value = newVal;
}
console.log('我被改变了');
// todo: 这里可以发布通知
}
})
}
var user = {};
defineReactive(user, 'name', 'abc');
try {
// 尝试还原State
let state = JSON.parse(sessionStorage.getItem('app_state'));
if (state && Object.keys(state).length > 0) {
store.replaceState(state);
}
} catch (e) {}
const vm = new Vue({
el: '#app',
store,
render: h => h(App)
});
window.addEventListener('beforeunload', function (e) {
// 关闭或刷新时,保存State
var currentState = Object.assign({}, vm.$store.state);
sessionStorage.setItem('app_state', JSON.stringify(currentState));
}, false);