1--項(xiàng)目起步

一 . 啟動(dòng)并運(yùn)行項(xiàng)目

a. 通過cmd命令
?cd /myvue進(jìn)入項(xiàng)目目錄
?npm run serve
b. 通過VScode/HB等
?Ctrl+" ` "
?npm run serve

二 . 組件創(chuàng)建和使用

創(chuàng)建組件 3要素:

  1. <template>有且只能有一個(gè)根div </template>
  2. <script></script>
  3. <style></style>

使用組件 3步驟

  1. 導(dǎo)入組件
    import Header from './components/Header.vue' (.vue可省略)
  2. 注冊(cè)組件
    components:{Header}
  3. 使用組件
    <header></header>
<template>
  <div id="app">  
      <!-- 03 使用組件 -->
      <Header></Header>  
  </div>  
</template>
<script>
// 01 導(dǎo)入組件
import Header from './components/Header.vue'
export default {
// 02 注冊(cè)組件
  components:{Header,Nav,Swiper},
</script>
<style>
</style>

四、組件的參數(shù)傳遞

  1. App.vue屬性傳參 <Nav :navList="navList">
  2. Nav.vue props:["navList"]
<template>
  <div id="app">  
      <!-- 03 使用組件 -->
      <Header></Header>
      <Nav :navList="navList"></Nav>      
  </div>
</template>
<script>
// 01 導(dǎo)入組件
import Nav from './components/Nav.vue'
export default {
// 02 注冊(cè)組件
  components:{Nav},
  data(){
    return {
      navList:[
        {name:"推薦"},
        {name:"手機(jī)"},
        {name:"智能"},
        {name:"電視"},
        {name:"筆記本"},
        {name:"家電"},
        {name:"生活周邊"},
      ]   
   }}
}
</script>
<template> 
    <div class="btn" v-for="(item,index) in navList" :key="index">
         {{item.name}}
    </div>
</template>
<script>
export default {
    props:["navList"],
}
</script>

五厘肮、 樣式隔離

<style scoped>

六、vue中獲取dom元素

   templte 定義ref  <div ref="scroll">
   script    this.$refs.scroll 

七、 vue 內(nèi)置參數(shù)

    data(){return {}} 數(shù)據(jù)
    methos() 方法
    props:[]  屬性(父?jìng)鬟f過來參數(shù))

八 . 生命周期鉤子函數(shù)

生命周期鉤子函數(shù)作用
比如我進(jìn)入組件要發(fā)起ajax?
組件渲染好了我要調(diào)用dom操作烁落?
進(jìn)入組件要加入定時(shí)器嘹锁?
離開組件要銷毀定時(shí)器?
要知道組件視圖發(fā)生改變剧董?

創(chuàng)建 渲染 更新 銷毀 前后8個(gè)
beforeCreate 創(chuàng)建前
created 創(chuàng)建完成(可以使用this)
beforeMount 渲染前
mounted 渲染后(可以操作dom)
beforeUpdate 更新前(執(zhí)行多次)
updated 更新后(執(zhí)行多次)
beforeDestroy 銷毀前
destroyed 銷毀后

9. nav選項(xiàng)卡組件

<template>
    <div class="nav-wrap">
        <div class="nav" ref="scroll">
            <div 
            :class="{'active':index==current}"
            @click="selected($event,index)"
            v-for="(item,index) in navList"
            :key="index"
            class="nav-item">
                {{item.name}}
            </div>
        </div>
        <div class="nav-toggle"
        :class="{'up':showLay==true}"
         @click="showLay=!showLay">
            >
        </div>
        <div class="nav-lay" v-if="showLay">
            <div class="nav-title">標(biāo)題</div>
            <div class="nav-buttons">
                <div class="btn" 
                @click="btnclick(index)"
                :class="{'active':index==current}"
                v-for="(item,index) in navList"
                :key="index"
                >{{item.name}}</div>
            </div>
        </div>
    </div>
</template>
<script>
export default {
    props:["navList"],
    methods:{
        btnclick(index){
           this.showLay=false;
        //  隱藏彈出菜單按鈕
           this.current=index;
        //  設(shè)置當(dāng)前選中current
           let items = document.querySelectorAll(".nav-item");
        // 獲取到所有的.nav-item
           this.selected({target:items[index]},index);
        // 執(zhí)行selected 方法({target:和index對(duì)應(yīng)的nav-item},index);
        //  執(zhí)行selected函數(shù)傳兩個(gè)參數(shù) 
        // 第一個(gè)參數(shù)是個(gè)對(duì)象 對(duì)象里面有個(gè)target屬性 屬性值是一個(gè)dom節(jié)點(diǎn)(.nav-item 對(duì)應(yīng)current的dom節(jié)點(diǎn)) 
        // 比如我單擊的是btns里面的第2個(gè)元素 讓后我選擇到nav-item里面的第2個(gè)元素作為target屬性值傳遞過去
        // 第二個(gè)參數(shù)是index
        },
        selected(e,index){
            this.current = index;
            let ew = e.target.offsetWidth; // 獲取當(dāng)前單擊元素寬
            let el = e.target.offsetLeft;// 元素左側(cè)偏移值
            let elem = this.$refs.scroll; // 獲取滾動(dòng)元素; $refs是vue內(nèi)置的獲取元素對(duì)象
            let vw = elem.offsetWidth;// 滾動(dòng)元素整體的寬;
            // console.log(ew,el,vw);
            elem.scrollLeft = el-vw/2+ew/2;
            // 設(shè)置滾動(dòng)元素的左側(cè)滾動(dòng)距離為=調(diào)整單擊元素到畫面的中心破停。
            // 單擊的元素滾動(dòng)到最左側(cè)
            // 最左側(cè)-總寬度一半+當(dāng)前元素寬度一半
        }
    },
    data(){return {
        current:0,// 當(dāng)前選中的導(dǎo)航
        showLay:false,//是否顯示導(dǎo)航彈出內(nèi)容
    }}
}
</script>
<style  scoped>
/* scoped 讓樣式只在當(dāng)前組件中管用 */
.nav-buttons .btn.active{
    background-color:#ffe8d5;
    color:#f30;
}
.nav-buttons .btn{
    width: 20%; height: 30px; line-height: 30px; margin-bottom: 15px; margin-right: 15px; border-radius: 6px; background-color:#fff; border:1px solid #eee; display: inline-block; text-align: center; color:#777; font-size:14px;
}
.nav-lay{
    position: absolute; left:0; top:44px; width: 100%;
}
.nav-title{
    margin-right: 44px; height: 44px; background-color: #f0f0f0; line-height: 44px; padding-left: 15px; margin-top: -44px; }
.nav-buttons{
    padding: 0 15px;
  
    background-color: #f0f0f0;
}
 
.nav-toggle{
    width: 44px;
    height: 44px;
    line-height: 44px;
    text-align: center;
    color:#777;
    background-color: #f0f0f0;
    transform: rotate(90deg);
    /* transition:all ease .4s; */
}
.up{
     transform: rotate(-90deg);
}
.nav-wrap{
    width: 100%;
    height: 44px;
    display: flex;
    position: relative;
    z-index: 10000;
    background-color: #f0f0f0;
}
.nav{
    flex:1;
    line-height: 44px;
    background-color: #f0f0f0;
    overflow-x:auto;
    /* 水平滾動(dòng)自動(dòng) */
    white-space: nowrap;
    /* 行內(nèi)元素不換行 */
    scroll-behavior: smooth; 
      /* 滾動(dòng)平滑 */
  -webkit-overflow-scrolling: touch;
 
  
}
.nav-item{
    display: inline-block;
    /* 轉(zhuǎn)換位行內(nèi)塊 */
    vertical-align: middle;
    /* 垂直居中對(duì)齊 */
    padding: 0 15px;
    /* 設(shè)置間距 */
    color:#484848;
    /* 設(shè)置顏色 */
}
::-webkit-scrollbar{
    display: none;
}
/* 隱藏滾動(dòng)條 */

.active{color:#f30;}
</style>

引入外部插件組件的一般方法

swiper為例子

  1. 工作目錄 cmd安裝

myvue> npm install swiper -S

  1. 使用的組件里面 導(dǎo)入
import Swiper from 'swiper'
import "swiper/css/swiper.css"

如果引入的目錄沒有./ ../等相對(duì)目錄翅楼,那么就會(huì)從 node_module查找需要引用的內(nèi)容

  1. 樣式

import ‘xxx/swiper.js’

  1. dom 結(jié)構(gòu)
    .swiper-contianer
    --- .swiper.wrappr
    --- --- .swiper-slide
    1. 初始化swiper
      new Swiper(‘.swiper-container’)

swiper組件是要操作dom的,我就應(yīng)該在組件生命周期
mounted 完畢 取執(zhí)行 初始化 swiper

10. swiper組件

<template>
    <div class="swiper-container" v-if="gallery.length">
        <!-- 只有g(shù)allery有長(zhǎng)度時(shí)候才顯示swiper -->
        <div class="swiper-wrapper">
            <div class="swiper-slide" 
            v-for="(item,index) in gallery"
            :key="index">
                <img :src="item.url" alt="" width="100%">
            </div>
            
        </div>
        <div class="swiper-pagination"></div>
    </div>
</template>
<script>
import Swiper from 'swiper'
import "swiper/css/swiper.css"
// 導(dǎo)入node_modules里面的 swiper文件夾 Swiper類
// 導(dǎo)入node_modules里面的 swiper/css/文件夾里面的swiper.css
export default {
    props:["gallery"],
    mounted(){
        // 當(dāng)頁(yè)面初始渲染完畢執(zhí)行
        this.swiper = new Swiper(".swiper-container",{
            loop:true,//自動(dòng)讓最后一張的下一張是第一張
            pagination:{
                el:'.swiper-pagination'//小點(diǎn)指示器
            }
        });
    }
}
</script>
<style>
    .swiper-container{
        height: 200px;
        width: 100%;
    }
    /* 設(shè)置幻燈片高與寬 */
    /* 修改小點(diǎn)高亮顏色 */
    .swiper-pagination-bullet-active{
        background-color: #f30;
    }
</style>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末真慢,一起剝皮案震驚了整個(gè)濱河市毅臊,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌黑界,老刑警劉巖管嬉,帶你破解...
    沈念sama閱讀 219,188評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異园爷,居然都是意外死亡宠蚂,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,464評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門童社,熙熙樓的掌柜王于貴愁眉苦臉地迎上來求厕,“玉大人,你說我怎么就攤上這事⊙窖ⅲ” “怎么了美浦?”我有些...
    開封第一講書人閱讀 165,562評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)项栏。 經(jīng)常有香客問我浦辨,道長(zhǎng),這世上最難降的妖魔是什么沼沈? 我笑而不...
    開封第一講書人閱讀 58,893評(píng)論 1 295
  • 正文 為了忘掉前任流酬,我火速辦了婚禮,結(jié)果婚禮上列另,老公的妹妹穿的比我還像新娘芽腾。我一直安慰自己,他們只是感情好页衙,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,917評(píng)論 6 392
  • 文/花漫 我一把揭開白布摊滔。 她就那樣靜靜地躺著,像睡著了一般店乐。 火紅的嫁衣襯著肌膚如雪艰躺。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,708評(píng)論 1 305
  • 那天眨八,我揣著相機(jī)與錄音腺兴,去河邊找鬼。 笑死踪古,一個(gè)胖子當(dāng)著我的面吹牛含长,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播伏穆,決...
    沈念sama閱讀 40,430評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼纷纫!你這毒婦竟也來了枕扫?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,342評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤辱魁,失蹤者是張志新(化名)和其女友劉穎烟瞧,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體染簇,經(jīng)...
    沈念sama閱讀 45,801評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡参滴,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,976評(píng)論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了锻弓。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片砾赔。...
    茶點(diǎn)故事閱讀 40,115評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出暴心,到底是詐尸還是另有隱情妓盲,我是刑警寧澤,帶...
    沈念sama閱讀 35,804評(píng)論 5 346
  • 正文 年R本政府宣布专普,位于F島的核電站悯衬,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏檀夹。R本人自食惡果不足惜筋粗,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,458評(píng)論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望炸渡。 院中可真熱鬧亏狰,春花似錦、人聲如沸偶摔。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,008評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)辰斋。三九已至策州,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間宫仗,已是汗流浹背够挂。 一陣腳步聲響...
    開封第一講書人閱讀 33,135評(píng)論 1 272
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留藕夫,地道東北人孽糖。 一個(gè)月前我還...
    沈念sama閱讀 48,365評(píng)論 3 373
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像毅贮,于是被迫代替她去往敵國(guó)和親办悟。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,055評(píng)論 2 355

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