vue Blog 學習筆記 (1) 安裝、頁面顯示

PJ Blog

// 項目根目錄下執(zhí)行
npm run watch

> @ watch D:\wamp\www\blog

> cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --watch --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js
app
resources
  assets
    - js
  lang
  views
  • webpack.min.js
    解析模塊時應該搜索的目錄
解析模塊好復雜啊,到處都有
mix.webpackConfig({
    resolve: {
        alias: {
            'components': 'assets/js/components',
            'config': 'assets/js/config',
            'lang': 'assets/js/lang',
            'plugins': 'assets/js/plugins',
            'vendor': 'assets/js/vendor',
            'views': 'assets/js/views',
            'dashboard': 'assets/js/views/dashboard',
        },
        modules: [
          'node_modules',
          path.resolve(__dirname, "resources")
        ]
    },
})

resolve.alias
創(chuàng)建 import 或 require 的別名,來確保模塊引入變得更簡單得封。例如,一些位于 src/ 文件夾下的常用模塊:

resolve.alias

resolve.modules
告訴 webpack 解析模塊時應該搜索的目錄指郁。
絕對路徑和相對路徑都能使用忙上,但是要知道它們之間有一點差異。通過查看當前目錄以及祖先路徑(即 ./node_modules, ../node_modules 等等)闲坎,相對路徑將類似于 Node 查找 'node_modules' 的方式進行查找晨横。

使用絕對路徑洋腮,將只在給定目錄中搜索。

resolve.modules defaults to:
modules: ["node_modules"]

如果你想要添加一個目錄到模塊搜索目錄手形,此目錄優(yōu)先于 node_modules/ 搜索:

modules: [path.resolve(__dirname, "src"), "node_modules"]
  • 多語言 vue-i18n
/resources/assets/js/views/dashboard/System.vue
$t 怎么實現(xiàn)的哩
{{ $t(pages.systems) }}

/resources/assets/js/app.js
  import locales from 'lang'
  import VueI18n from 'vue-i18n';
  Vue.config.lang = Window.language
  const i18n = new VueI18n({
    locale: Vue.config.lang,
    messages: locales
  })
  • 為什么要用兩套路由系統(tǒng)
    // vue 路由
    resources/assets/js/routers.js

    // php 交互路由
    routes/api.php

    routers.js是vue的路由,是前端界面;
    api.php是后端路由

  • dashboard 后臺首頁
http://blog.app/dashboard/home

路由
resources/assets/js/routes.js
  export defaults [
    path: '/dashboard',
    component: Dashboard,
    beforeEnter: requireAuth,
    children: [
      {
        path: '/',
        redirect: '/dashboard/home'
      },
      {
        path: '/home',
        component: require('dashboard/Home.vue')
        // 此處的 dashboard 即為之前配置的解析別名模塊
        // 'dashboard': 'assets/js/views/dashboard',
      }
      ......
    ]
  ]

resources/assets/js/views/dashboard/Home.vue
export default {
    components: {
        Chart
    },
    data () {
        return {
            statistics: {}
        }
    },
    mounted() {
        this.$http.get('statistics')
            .then((response) => {
                this.statistics = response
            })
    }
}
路由 statistics 在 api.php 路由文件中定義
請求響應包含以下內容
{
  // `data` 由服務器提供的響應
  data: {},

  // `status` 來自服務器響應的 HTTP 狀態(tài)碼
  status: 200,

  // `statusText` 來自服務器響應的 HTTP 狀態(tài)信息
  statusText: 'OK',

  // `headers` 服務器響應的頭
  headers: {},

  // `config` 是為請求提供的配置信息
  config: {}
}

this.$http.get
vue 不再推薦使用 vue-resource, 推薦使用 axios

resources/assets/js/plugins/http/index.js
該文件實現(xiàn) Vue 中 this.$http.get() 使用 axios 庫
  • vue-table
/resources/assets/js/components/particals/Table.vue
<vue-table></vue-table>

resources/assets/js/app.js
Vue.component(
    'vue-table',
    require('components/dashboard/Table.vue')
);

resources/assets/js/components/dashboard/Table.vue

  • vue-router 路由插件
組件 vue-router
<div id="app">
  <h1>Hello App!</h1>
  <p>
    <!-- use router-link component for navigation. -->
    <!-- specify the link by passing the `to` prop. -->
    <!-- `<router-link>` will be rendered as an `<a>` tag by default -->
    <router-link to="/foo">Go to Foo</router-link>
    <router-link to="/bar">Go to Bar</router-link>
  </p>
  <!-- route outlet -->
  <!-- component matched by the route will render here -->
  <router-view></router-view>
</div>

<router-link> automatically gets the .router-link-active class 
when its target route is matched

<router-view></router-view>
Nested Routes
<div id="app">
  <router-view></router-view>
</div>
const User = {
  template: '<div>User {{ $route.params.id }}</div>'
}
const router = new VueRouter({
  routes: [
    { path: '/user/:id', component: User }
  ]
})


The <router-view> here is a top-level outlet. 
It renders the component matched by a top level route. 
Similarly, a rendered component can also contain its own, nested <router-view>. 
For example, if we add one inside the User component's template:

const User = {
  template: `
    <div class="user">
      <h2>User {{ $route.params.id }}</h2>
      <router-view></router-view>
    </div>
  `
}

后臺的控制器放在 Api 目錄,思想就是當做接口的形式給 Vue 返回數據呀
Fractal provides a presentation and transformation layer for complex data output, the like found in RESTful APIs,
and works really well with JSON. Think of this as a view layer for your JSON/YAML/etc.

  • User.vue 用戶列表首頁
    還不知道怎讀請求的數據過來呀!

包含 Table.vue 文件
loadData() {
// User.vue 文件中 <vue-table></vue-table> 節(jié)點有屬性 api-url="user"
var url = this.apiUrl // 從哪里取的值

}

resources/assets/js/views/dashboard/user/User.vue

<template>
    <div class="row">
        <vue-table :title="$t('page.users')" :fields="fields" api-url="user" 
            @table-action="tableActions" show-paginate>
            <div slot="buttons">
                <router-link to="/dashboard/users/create" class="btn btn-success">
                  {{ $t('page.create') }}
                </router-link>
            </div>
        </vue-table>
    </div>
</template>
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末往枣,一起剝皮案震驚了整個濱河市颤芬,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌坦仍,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,657評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異艘虎,居然都是意外死亡,警方通過查閱死者的電腦和手機咒吐,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,889評論 3 394
  • 文/潘曉璐 我一進店門野建,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人恬叹,你說我怎么就攤上這事候生。” “怎么了绽昼?”我有些...
    開封第一講書人閱讀 164,057評論 0 354
  • 文/不壞的土叔 我叫張陵唯鸭,是天一觀的道長。 經常有香客問我硅确,道長目溉,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,509評論 1 293
  • 正文 為了忘掉前任菱农,我火速辦了婚禮缭付,結果婚禮上,老公的妹妹穿的比我還像新娘大莫。我一直安慰自己蛉腌,他們只是感情好,可當我...
    茶點故事閱讀 67,562評論 6 392
  • 文/花漫 我一把揭開白布只厘。 她就那樣靜靜地躺著烙丛,像睡著了一般。 火紅的嫁衣襯著肌膚如雪羔味。 梳的紋絲不亂的頭發(fā)上河咽,一...
    開封第一講書人閱讀 51,443評論 1 302
  • 那天,我揣著相機與錄音赋元,去河邊找鬼忘蟹。 笑死飒房,一個胖子當著我的面吹牛,可吹牛的內容都是我干的媚值。 我是一名探鬼主播狠毯,決...
    沈念sama閱讀 40,251評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼褥芒!你這毒婦竟也來了嚼松?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,129評論 0 276
  • 序言:老撾萬榮一對情侶失蹤锰扶,失蹤者是張志新(化名)和其女友劉穎献酗,沒想到半個月后,有當地人在樹林里發(fā)現(xiàn)了一具尸體坷牛,經...
    沈念sama閱讀 45,561評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡罕偎,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,779評論 3 335
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了京闰。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片颜及。...
    茶點故事閱讀 39,902評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖忙干,靈堂內的尸體忽然破棺而出器予,到底是詐尸還是另有隱情,我是刑警寧澤捐迫,帶...
    沈念sama閱讀 35,621評論 5 345
  • 正文 年R本政府宣布乾翔,位于F島的核電站,受9級特大地震影響施戴,放射性物質發(fā)生泄漏反浓。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,220評論 3 328
  • 文/蒙蒙 一赞哗、第九天 我趴在偏房一處隱蔽的房頂上張望雷则。 院中可真熱鬧,春花似錦肪笋、人聲如沸月劈。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,838評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽猜揪。三九已至,卻和暖如春坛梁,著一層夾襖步出監(jiān)牢的瞬間而姐,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,971評論 1 269
  • 我被黑心中介騙來泰國打工划咐, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留拴念,地道東北人钧萍。 一個月前我還...
    沈念sama閱讀 48,025評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像政鼠,于是被迫代替她去往敵國和親风瘦。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,843評論 2 354

推薦閱讀更多精彩內容