Vue全家桶 vue + vue-router + vuex

Vue實例的生命週期鉤子函數(8個)
        1. beforeCreate
              data屬性光聲明沒有賦值的時候
        2. created
              data屬性完成了賦值
        3. beforeMount
              頁面上的{{name}}尚未被渲染成真正的數據
        4. mounted
              頁面上的{{name}}被渲染成真正的數據
        5. beforeUpdate
              數據(data屬性)更新以前會執行的函數
        6. updated
              數據(data屬性)更新完會執行的函數
        7. beforeDestroy
              實例被銷燬以前會執行的函數
        8. destroyed
              實例被銷燬後會執行的函數javascript

 

Vue Router

Vue Router 是 Vue.js 官方的路由管理器。它和 Vue.js 的核心深度集成,讓構建單頁面應用變得易如反掌。包含的功能有:html

  • 嵌套的路由/視圖表
  • 模塊化的、基於組件的路由配置
  • 路由參數、查詢、通配符
  • 基於 Vue.js 過渡系統的視圖過渡效果
  • 細粒度的導航控制
  • 帶有自動激活的 CSS class 的連接
  • HTML5 歷史模式或 hash 模式,在 IE9 中自動降級
  • 自定義的滾動條行爲

2. VueRouter  https://router.vuejs.org/zh/
        1. 基本使用
            1. 必須導入vue-router.js文件
            2. 要有VueRouter()實例
            3. 要把VueRouter實例掛載到Vue實例中
            4. 路由的入口
                  <router-link to='/index'>index頁面</router-link>
            5. 路由的出口
                  <router-view></router-view>
        2. 路由的參數
            1. path: '/user/:name' --> 匹配路由
            $route.params.name     --> 取值
            
            2. /user/alex?age=9000 --> url中攜帶參數
            $route.query.age       --> 取出url的參數
            
        3. 子路由
            children:[
                {
                    path: '',
                    component: {
                        template: `...`
                    }
                }
            ]
            <router-link to='info' append></router-link>
4. 編程式導航
            用JS代碼去控制頁面跳轉
            this.$router.push(...)vue

5. 組件中捕獲原生事件 :有兩個組件,其中一個捕獲另外一個事件
        .native修飾符java

  v-on:click.native='haha'webpack

components:{
                cym:{
                    template: `<button>點我</button>`
                },
                dbg:{
                    template: `<button v-on:click='dian'>大表哥</button>`,
                    methods:{
                        dian:function(){
                            // 手動拋出一個點擊事件
                            this.$emit('click')
                        }
                    }
                }
            },
            methods:{
                haha:function(){
                    alert(123)
                }
            }web

六、marked包的使用
        1. 安裝
            npm install marked -D
        2. 使用
            marked(content)vue-router

<script>
    import marked from 'marked'
    export default {
        name: 'Zsq',
        data:function(){
            return {
                content: ''
            }
        },
        computed:{
            markedDownContent:function(){
                return marked(this.content)
            }
        }
    }
</script>


  7. 補充問題
        0. npm安裝的問題
            1. npm install xx -D         --> 安裝當前項目用到的開發環境(好比webpack等)
            2. npm install xx -S(--save) --> 安裝當前項目用到的依賴包(線上運行必需要有的包)
            3. npm install xx -g         --> 全局安裝,安裝完以後在cmd能夠直接當命令行工具使用的
            
        1. Vue中配置全局jQuerynpm

 

用 Vue.js + Vue Router 建立單頁應用,是很是簡單的。使用 Vue.js ,咱們已經能夠經過組合組件來組成應用程序,當你要把 Vue Router 添加進來,咱們須要作的是,將組件 (components) 映射到路由 (routes),而後告訴 Vue Router 在哪裏渲染它們。下面是個基本例子:編程

HTML

<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>

<div id="app">
  <h1>Hello App!</h1>
  <p>
    <!-- 使用 router-link 組件來導航. -->
    <!-- 經過傳入 `to` 屬性指定連接. -->
    <!-- <router-link> 默認會被渲染成一個 `<a>` 標籤 -->
    <router-link to="/foo">Go to Foo</router-link>
    <router-link to="/bar">Go to Bar</router-link>
  </p>
  <!-- 路由出口 -->
  <!-- 路由匹配到的組件將渲染在這裏 -->
  <router-view></router-view>
</div>

JavaScript

// 0. 若是使用模塊化機制編程,導入Vue和VueRouter,要調用 Vue.use(VueRouter) // 1. 定義 (路由) 組件。 // 能夠從其餘文件 import 進來 const Foo = { template: '<div>foo</div>' } const Bar = { template: '<div>bar</div>' } // 2. 定義路由 // 每一個路由應該映射一個組件。 其中"component" 能夠是 // 經過 Vue.extend() 建立的組件構造器, // 或者,只是一個組件配置對象。 // 咱們晚點再討論嵌套路由。 const routes = [ { path: '/foo', component: Foo }, { path: '/bar', component: Bar } ] // 3. 建立 router 實例,而後傳 `routes` 配置 // 你還能夠傳別的配置參數, 不過先這麼簡單着吧。 const router = new VueRouter({ routes // (縮寫) 至關於 routes: routes }) // 4. 建立和掛載根實例。 // 記得要經過 router 配置參數注入路由, // 從而讓整個應用都有路由功能 const app = new Vue({ router }).$mount('#app') 
相關文章
相關標籤/搜索