美文网首页
Vue 创建单页面程序

Vue 创建单页面程序

作者: 骑代码奔小康 | 来源:发表于2018-11-21 12:08 被阅读0次

作为一个小公司的PHPER,不喜欢乌漆墨黑的命令行(老是报错,技术又不好找不到解决方法),研究了半天vue-click以后,选择了放弃,这里用vue创建一个单页面程序,不多说上代码!

引入vue相关的CDN,建议在https://www.bootcdn.cn/去找,

  1. vue.js的CDN https://cdn.bootcss.com/vue/2.5.17-beta.0/vue.js
  2. vue的路由 https://cdn.bootcss.com/vue-router/3.0.1/vue-router.js
  3. vue状态管理vuex https://cdn.bootcss.com/vuex/3.0.1/vuex.js

html代码只有body 部分

<body>
    <div id="app">
        <div class="row">
            <div class="col-xs-offset-2 col-xs-8">
                <div class="page-header">
                    <h2>Router Test</h2>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-xs-2 col-xs-offset-2">
                <div class="list-group">
                    <!--使用指令v-link进行导航-->
                    <router-link to='/home' class="list-group-item"  >Home</router-link>
                    <router-link to='/about'  class="list-group-item"  >About</router-link>
                </div>
            </div>
            <div class="col-xs-6">
                <div class="panel">
                    <div class="panel-body">
                        <!--用于渲染匹配的组件-->
                        <router-view></router-view>
                    </div>
                </div>
            </div>
        </div>
    </div>
   // home路由部分
    <template id="home">
        <div>
            <h1 @click="_alert(123)">Home</h1>
            <p>{{num}}</p>
        </div>
    </template>
  // about路由部分
    <template id="about">
        <div>
            <h1>About</h1>
            <p>{{num}}</p>
        </div>
    </template>
</body>

js部分的代码

<script>
/* 创建一个vuex状态管理 */
/* 调用Vuex store 的方式只介绍两种  
* 同步的方式  store.commit('increment','需要传的参数')
* 异步的方式  store.dispatch('incrementAsync',{ amount: 10  })
* 直接在组件中使用就行
*/
const store = new Vuex.Store({
    state: {
        count: 5
    },
    mutations: {/*实际改变参数状态的方法*/
        increment (state, no) {
            state.count = Number(state.count) + Number(no)
        }
    },
    actions: {  /*按我的理解是在actions中调用mutations的方法increment实现异步传输给组件*/
        incrementAsync  (context,payload) {
            context.commit('increment',payload.amount)
        }
    }
})


/* 创建Home 组件构造器  */
const Home = Vue.extend({
    template: '#home',
    data: function () {
        return {
            msg: 'Hello, vue router!',
            num: ''
        }
    },
    methods:{
        _alert:function (e){
            store.dispatch('incrementAsync',{
                amount: 10
            }) /* 异步的方式调用vuex */
            this.num = store.state.count  /*获取vuex中count的值*/ 
            console.log( this.num )
        }
    },
    created:function(){
        this.num = store.state.count
        console.log( this.num ) // -> 1
    }
})

/* About 路由的组件*/
const About = Vue.extend({
    template: '#about',
    data: function () {
        return {
            msg: 'Hello, vue router6666666666!',
            num: store.state.count
        }
    },
    methods:{
        _alert:function(e){
            alert(e)
        }
    },
})

/* 创建路由映射  */
const routes = [
    { path: '/home', component: Home},
    { path: '/about', component: About },
    { path: '/', component: Home }
]

/* 创建路由器  */
var router = new VueRouter({
    routes
})

//  创建一个顶层的vue实例
const vm = new Vue({
  // el: 'body',
  router, //  路由
  components: { Home, About },
  template: '#app'
})

/* 启动路由  */
const app = new Vue({
    router
}).$mount('#app')
</script>

整个完整页面的代码

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title></title>
    <link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css" />
</head>
<body>
    <div id="app">
        <div class="row">
            <div class="col-xs-offset-2 col-xs-8">
                <div class="page-header">
                    <h2>Router Basic - 01</h2>
                </div>
            </div>
        </div>
        <div class="row">
            <div class="col-xs-2 col-xs-offset-2">
                <div class="list-group">
                    <!--使用指令v-link进行导航-->
                    <router-link to='/home' class="list-group-item"  >Home</router-link>
                    <router-link to='/about'  class="list-group-item"  >About</router-link>
                </div>
            </div>
            <div class="col-xs-6">
                <div class="panel">
                    <div class="panel-body">
                        <!--用于渲染匹配的组件-->
                        <router-view></router-view>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <template id="home">
        <div>
            <h1 @click="_alert(123)">Home</h1>
            <p>{{num}}</p>
        </div>
    </template>
    <template id="about">
        <div>
            <h1>About</h1>
            <p>{{num}}</p>
        </div>
    </template>
</body>
<script src="./js/vue.js"></script>
<script src="./js/vue-router.js"></script>
<script src="https://cdn.bootcss.com/vuex/3.0.1/vuex.js"></script>
<script>
const store = new Vuex.Store({
    state: {
        count: 5
    },
    mutations: {
        increment (state, no) {
            state.count = Number(state.count) + Number(no)
        }
    },
    actions: {
        incrementAsync  (context,payload) {
            context.commit('increment',payload.amount)
        }
    }
})

/* 创建组件构造器  */
const Home = Vue.extend({
    template: '#home',
    data: function () {
        return {
            msg: 'Hello, vue router!',
            num: ''
        }
    },
    methods:{
        _alert:function (e){
            store.dispatch('incrementAsync',{
                amount: 10
            })
            this.num = store.state.count
            console.log( this.num )
        }
    },
    created:function(){
        this.num = store.state.count
        console.log( this.num ) // -> 1
    }
})
/* About 路由的组件 */
const About = Vue.extend({
    template: '#about',
    data: function () {
        return {
            msg: 'Hello, vue router6666666666!',
            num: store.state.count
        }
    },
    methods:{
        _alert:function(e){
            alert(e)
        }
    },
})

/* 创建路由映射  */
const routes = [
    { path: '/home', component: Home},
    { path: '/about', component: About },
    { path: '/', component: Home }
]

/* 创建路由器  */
var router = new VueRouter({
    routes
})

const vm = new Vue({
  // el: 'body',
  router,
  components: { Home, About },
  template: '#app'
})

/* 启动路由  */
const app = new Vue({
    router
}).$mount('#app')
</script>
</html>

相关文章

  • Vue 创建单页面程序

    作为一个小公司的PHPER,不喜欢乌漆墨黑的命令行(老是报错,技术又不好找不到解决方法),研究了半天vue-cli...

  • uni-app页面布局

    单页面程序 uni-app 约定页面文件遵循 Vue 单文件组件 (SFC) 规范[https://vue-loa...

  • vue路由

    路由 路由:vue-routervue的核心插件vue-router.js根据不同的url访问不同的页面创建单页面...

  • 10. 基于Vue+Element+nodeJs+Express

    一、创建vue单页面项目 我这里创建vue项目使用的是Vue-CLI脚手架,Vue CLI 是一个基于 Vue.j...

  • 2020-02-14

    vue-router的使用:开发vue单页面程序时,app.vue只是

  • 路由

    路由(VueRouter) 1.vue的核心插件 根据不同的url访问不同的页面 创建单页面SPA(SINGLE ...

  • 【Vue】学习vue前的几个常见问题(part2)

    1.Vue ≠ 单页面应用 Vue可以用来做单页面应用,但是不表示Vue只能用来做单页面应用。Vue完全支持传统网...

  • 路由

    vue的核心插件 1,根据不同的url访问不同的页面 2, 创建单页面SPA(SINGLE PAGE APPLI...

  • 2019最新微信小程序开发制作模板

    闪云科技小程序,包含多样化的模板和可视化操作页面,能够快速创建各类版本小程序。 单页版: 单页版小程序是单页面的广...

  • Vue路由设置mode模式为History 中页面404的原因及

    我们使用vue+vue-router创建单页面应用的时候,一般会在router中设置mode:history,让我...

网友评论

      本文标题:Vue 创建单页面程序

      本文链接:https://www.haomeiwen.com/subject/etwqqqtx.html