Vue項目篇-01

1杀狡、首先下載項目

  • npm install -g vue-cli-------全局安裝vue-cli
  • vue init webpack project-name-------webpack初始化項目
  • cd project-name---------進入項目
  • npm install------------webpack安裝需要的包
  • npm run dev-------------自動創(chuàng)建服務(wù)器并運行項目

2宾袜、代碼的編寫

  • import/export----------ES6導(dǎo)入與導(dǎo)出

  • 創(chuàng)建hello.vue組件

每一個頁面都是一個組件价捧,都是一個vue文件抗悍,由template埠褪、JavaScript诞丽、style構(gòu)成

template:<code><template></template></code>

注意:template標(biāo)簽內(nèi)需要一個根標(biāo)簽鹉勒,template最后會消失

JavaScript:<code><script></script></code>

注意:用export default{}-------模塊默認(rèn)導(dǎo)出

style:<code><style></style></code>

編寫有關(guān)當(dāng)前頁面樣式

3磅轻、路由 vue-router

用 Vue.js + vue-router 創(chuàng)建單頁應(yīng)用珍逸,是非常簡單的。使用 Vue.js 時聋溜,我們就已經(jīng)把組件組合成一個應(yīng)用了谆膳,當(dāng)你要把 vue-router 加進來,只需要配置組件和路由映射勤婚,然后告訴 vue-router 在哪里渲染它們摹量。

vue-router鏈接

  • 安裝-------------npm install vue-router

  • 引入-------------import VueRouter from “vue-router”

  • 調(diào)用-------------Vue.use(VueRouter)

  • 使用

    • <code> <router-link to='/home'></router-link> </code>
      最后會被渲染成a標(biāo)簽,通過to屬性進行錨點的設(shè)置
    • <code><router-view></router-view></code>
    • 編寫路由代碼

html

<div id="app">
  <h1>Hello App!</h1>
  <p>
    <!-- 使用 router-link 組件來導(dǎo)航. -->
    <!-- 通過傳入 `to` 屬性指定鏈接. -->
    <!-- <router-link> 默認(rèn)會被渲染成一個 `<a>` 標(biāo)簽 -->
    <router-link to="/foo">Go to Foo</router-link>
    <router-link to="/bar">Go to Bar</router-link>
  </p>
  <!-- 路由出口 -->
  <!-- 路由匹配到的組件將渲染在這里 -->
  <router-view></router-view>
</div>

JS代碼

// 0. 如果使用模塊化機制編程馒胆,導(dǎo)入Vue和VueRouter缨称,要調(diào)用 Vue.use(VueRouter)

// 1. 定義(路由)組件。
const Foo = { template: '<div>foo</div>' }
const Bar = { template: '<div>bar</div>' }
// 可以從其他文件 import 進來
import Home from ‘./pages/home’
import Mine from ‘./pages/mine’

// 2. 定義路由
// 每個路由應(yīng)該映射一個組件祝迂。 其中"component" 可以是
// 通過 Vue.extend() 創(chuàng)建的組件構(gòu)造器睦尽,
// 或者,只是一個組件配置對象型雳。
// 我們晚點再討論嵌套路由当凡。
const routes = [
  { path: '/foo', component: Foo },
  { path: '/bar', component: Bar },
  { path: '/home', component: Home},
  { path: '/mine', component: Mine}
]

// 3. 創(chuàng)建 router 實例山害,然后傳 `routes` 配置
// 你還可以傳別的配置參數(shù), 不過先這么簡單著吧。
const router = new VueRouter({
  routes // (縮寫)相當(dāng)于 routes: routes
})

// 4. 創(chuàng)建和掛載根實例沿量。
// 記得要通過 router 配置參數(shù)注入路由浪慌,
// 從而讓整個應(yīng)用都有路由功能
const app = new Vue({
  router
}).$mount('#app')

//當(dāng)然也可以這樣寫
const app = new Vue({
    el:'#app',
    template:'<App/>',
    components:{App},
    router
})
// 現(xiàn)在,應(yīng)用已經(jīng)啟動了朴则!

要注意:當(dāng)<code><router-link></code>對應(yīng)的路由匹配成功权纤,將自動設(shè)置class屬性值.router-link-active。查看API文檔

下面展示一下大牛寫的完整的代碼吧乌妒!

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style type="text/css">
        .router-link-active {
          color: red;
        }
    </style>
</head>
<body>
    <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>
        <!-- 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>
    
    <script type="text/javascript">
        // 0. If using a module system, call Vue.use(VueRouter)

        // 1. Define route components.
        // These can be imported from other files
        const Foo = { template: '<div>foo</div>' }
        const Bar = { template: '<div>bar</div>' }

        // 2. Define some routes
        // Each route should map to a component. The "component" can
        // either be an actual component constructor created via
        // Vue.extend(), or just a component options object.
        // We'll talk about nested routes later.
        const routes = [
          { path: '/foo', component: Foo },
          { path: '/bar', component: Bar }
        ]
        // 3. Create the router instance and pass the `routes` option
        // You can pass in additional options here, but let's
        // keep it simple for now.
        const router = new VueRouter({
          routes
        })
        // 4. Create and mount the root instance.
        // Make sure to inject the router with the router option to make the
        // whole app router-aware.
        const app = new Vue({
          router
        }).$mount('#app')
        // Now the app has started!
    </script>
</body>
</html>

4汹想、設(shè)置二級路由

  • 子頁面中正常編寫router-link和router-view

      <div class="menu">
          <router-link to="mine/one">ONE</router-link>
          <router-link to="mine/two">TWO</router-link>
      </div>
      <router-view></router-view>
    
  • 配置二級路由

      const routers = [
          {path:'/mine',component:Mine,children:[
              {path:'one',component:One},
              {path:'two',component:Two}
          ]}
      ]
    

5、處理網(wǎng)絡(luò)請求

  • 安裝插件----------npm install vue-resource

  • 引入并使用
    import VueResource from 'vue-resource';
    Vue.use(VueResource);

  • 代碼

      mounted(){
          this.$http.get('http//www.vrserver.applinzi.com/aixianfeng/apihomehot.php').then(function(res){
              console.log(res);
          })
      }
    

這就是做一個完整項目所用到的全部了撤蚊,之后有時間我會做一個小項目做一個展示

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末古掏,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子侦啸,更是在濱河造成了極大的恐慌葵姥,老刑警劉巖颁糟,帶你破解...
    沈念sama閱讀 217,907評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件诱桂,死亡現(xiàn)場離奇詭異朱浴,居然都是意外死亡,警方通過查閱死者的電腦和手機顶捷,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,987評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來屎篱,“玉大人服赎,你說我怎么就攤上這事〗徊ィ” “怎么了重虑?”我有些...
    開封第一講書人閱讀 164,298評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長秦士。 經(jīng)常有香客問我缺厉,道長,這世上最難降的妖魔是什么隧土? 我笑而不...
    開封第一講書人閱讀 58,586評論 1 293
  • 正文 為了忘掉前任提针,我火速辦了婚禮,結(jié)果婚禮上曹傀,老公的妹妹穿的比我還像新娘辐脖。我一直安慰自己,他們只是感情好皆愉,可當(dāng)我...
    茶點故事閱讀 67,633評論 6 392
  • 文/花漫 我一把揭開白布嗜价。 她就那樣靜靜地躺著艇抠,像睡著了一般。 火紅的嫁衣襯著肌膚如雪久锥。 梳的紋絲不亂的頭發(fā)上家淤,一...
    開封第一講書人閱讀 51,488評論 1 302
  • 那天,我揣著相機與錄音瑟由,去河邊找鬼媒鼓。 笑死,一個胖子當(dāng)著我的面吹牛错妖,可吹牛的內(nèi)容都是我干的绿鸣。 我是一名探鬼主播,決...
    沈念sama閱讀 40,275評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼暂氯,長吁一口氣:“原來是場噩夢啊……” “哼潮模!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起痴施,我...
    開封第一講書人閱讀 39,176評論 0 276
  • 序言:老撾萬榮一對情侶失蹤擎厢,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后辣吃,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體动遭,經(jīng)...
    沈念sama閱讀 45,619評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,819評論 3 336
  • 正文 我和宋清朗相戀三年神得,在試婚紗的時候發(fā)現(xiàn)自己被綠了厘惦。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,932評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡哩簿,死狀恐怖宵蕉,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情节榜,我是刑警寧澤羡玛,帶...
    沈念sama閱讀 35,655評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站宗苍,受9級特大地震影響稼稿,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜讳窟,卻給世界環(huán)境...
    茶點故事閱讀 41,265評論 3 329
  • 文/蒙蒙 一让歼、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧挪钓,春花似錦是越、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,871評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽浦徊。三九已至,卻和暖如春天梧,著一層夾襖步出監(jiān)牢的瞬間盔性,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,994評論 1 269
  • 我被黑心中介騙來泰國打工呢岗, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留冕香,地道東北人。 一個月前我還...
    沈念sama閱讀 48,095評論 3 370
  • 正文 我出身青樓后豫,卻偏偏與公主長得像悉尾,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子挫酿,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,884評論 2 354

推薦閱讀更多精彩內(nèi)容