vue.js的方法

使用methods屬性

例子:method.html

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
<h2>{{greeting('Afternoon')}}</h2>
<h2>{{message}}</h2>
</div>
<script>
var app=new Vue({
el:'#app',
data:{
message:'hello Vue'
},
methods:{
greeting:function(time){
return 'good'+time+'!'

                }
                
            }
        })
    </script>
</body>

</html>
運(yùn)行結(jié)果

method.png

使用圖標(biāo)集

使用語法糖

v-on→@和v-on→:
例子:點(diǎn)我:v-on.html

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">
<button type="button" @click="handleClick">點(diǎn)我</button>
</div>
<script type="text/javascript">
var app=new Vue({
el:'#app',
data:{
name:'軟件1721'
},
methods:{
handleClick:function(){

                    alert(this.name);
                    }
                }
        }) 
    </script>
</body>

</html>

運(yùn)行結(jié)果:


4.png

隱藏和顯示1721:v-on1
<!DOCTYPE html>

<html>
<head>
<meta charset="utf-8">
<title>v-on指令練習(xí)-隱藏和顯示的切換練習(xí)</title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
<div id="app">

        <h2 v-if="show">{{name}}</h2>
        <button type="button" @click="handleClick">隱藏/顯示</button>
    </div>
    <script>
        var app=new Vue({
            el:'#app',
            data:{
                name:'軟件1721',
                show:true
            },
            methods:{
                handleClick:function(){
                    /* if(this.show===true){
                        this.show===false;
                    }else{
                        this.show===true
                    } */
                    this.show=!this.show;
                }
        }
        })
    </script>
</body></html>

運(yùn)行結(jié)果:
1.png
2.png

加一歲和減五歲:v-on2.html

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<title>年齡的加減</title>
</head>
<body>

    <div id="app">
        <h2>{{age}}</h2>
        <button type="button" @click="add">加一歲</button>
        <button type="button" @click="substrct(5)">減五歲</button>
    </div>
    <script type="text/javascript">
        var app = new Vue({
            el: '#app',
            data: {
                age: 30
            },
            methods: {
                add: function() {
                    /* if (this.show === true) {
                        this.show = false;
                    } else {
                        this.show = true;
                    } */
                    this.age += 1;
                },
                substrct: function(num) {
                    if (this.age < 5) {
                        alert("不能再減了");
                        return;
                    }
                    this.age -= num;
                }
            }
        })
    </script>
</body>

</html>

3.png

簡(jiǎn)書作者+關(guān)注:v-on3.html

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>

    <title>關(guān)注和取消關(guān)注</title>
    <link rel="stylesheet" type="text/css" href="css/font-awesome.min.css"/>
    <style type="text/css">
        .followed{
            color: #ddd;
        }
        .link{
            cursor: pointer;
        } 
        .cancle-followed{
            color: green;
        }
    </style>
</head>
<body>
    <div id="app">
        <h2>{{name}}</h2>
        <span class="followed link" v-show="followed" @click="handlefollow">
            <i class="icon-ok"></i>已關(guān)注
        </span>
        <span class="cancle-followed link" v-show="followed===false" @click="handlefollow">
            <i class="icon-plus"></i>關(guān)注
        </span>
    </div>
    <script type="text/javascript">
        var app = new Vue({
            el: '#app',
            data: {
                name:'簡(jiǎn)述作者',
                followed:false
            }, 
            methods: {
                handlefollow:function(){
                    this.followed=!this.followed;
                }
                }
        })
    </script>
</body>

</html>
運(yùn)行結(jié)果:


5.png

模仿簡(jiǎn)書下面點(diǎn)喜歡做的圖:likestyle.html

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<link rel="stylesheet" type="text/css" href="css/font-awesome.min.css"/>
<title>喜歡的樣式</title>
<style type="text/css">
.btn{
width: 100px;
height: 40px;
color: rgb(234,111,90);
border-radius: 20px;
font-size: 14px;
background-color: #FFFFFF;
border: 1px solid rgb(234,111,90);
outline:none;
}
</style>
</head>
<body>
<div id="app">
<button type="button" @click="handlefollow" class="btn">
<span class="followed link" v-show="followed" @click="handlefollow">
<i class="icon-heart"></i>喜歡 | {{number}}
</span>
<span class="cancle-followed link" v-show="followed===false" @click="handlefollow">
<i class="icon-heart-empty"></i>喜歡 | {{number}}
</span>
</button>
</div>
<script type="text/javascript">
var app = new Vue({
el: '#app',
data: {
followed:false,
number: 30
},
methods: {
add: function() {
/* if (this.show === true) {

                        this.show = false;
                    } else {
                        this.show = true;
                    } */
                    this.number += 1;
                },
                substrct: function(num) {
                    if (this.age < 5) {
                        alert("不能再減了");
                        return;
                    }
                    this.age -= num;
                }
            }
        })
    </script>
</body>

</html>

運(yùn)行結(jié)果:
6.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末荐糜,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子劫笙,更是在濱河造成了極大的恐慌他嚷,老刑警劉巖贪薪,帶你破解...
    沈念sama閱讀 222,946評(píng)論 6 518
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異富稻,居然都是意外死亡筒狠,警方通過查閱死者的電腦和手機(jī)舔庶,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,336評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門抛蚁,熙熙樓的掌柜王于貴愁眉苦臉地迎上來玲昧,“玉大人,你說我怎么就攤上這事篮绿。” “怎么了吕漂?”我有些...
    開封第一講書人閱讀 169,716評(píng)論 0 364
  • 文/不壞的土叔 我叫張陵亲配,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我惶凝,道長(zhǎng)吼虎,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 60,222評(píng)論 1 300
  • 正文 為了忘掉前任苍鲜,我火速辦了婚禮思灰,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘混滔。我一直安慰自己洒疚,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,223評(píng)論 6 398
  • 文/花漫 我一把揭開白布坯屿。 她就那樣靜靜地躺著油湖,像睡著了一般。 火紅的嫁衣襯著肌膚如雪领跛。 梳的紋絲不亂的頭發(fā)上乏德,一...
    開封第一講書人閱讀 52,807評(píng)論 1 314
  • 那天,我揣著相機(jī)與錄音吠昭,去河邊找鬼喊括。 笑死,一個(gè)胖子當(dāng)著我的面吹牛矢棚,可吹牛的內(nèi)容都是我干的郑什。 我是一名探鬼主播,決...
    沈念sama閱讀 41,235評(píng)論 3 424
  • 文/蒼蘭香墨 我猛地睜開眼幻妓,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼蹦误!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起肉津,我...
    開封第一講書人閱讀 40,189評(píng)論 0 277
  • 序言:老撾萬榮一對(duì)情侶失蹤强胰,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后妹沙,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體偶洋,經(jīng)...
    沈念sama閱讀 46,712評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,775評(píng)論 3 343
  • 正文 我和宋清朗相戀三年距糖,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了玄窝。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片牵寺。...
    茶點(diǎn)故事閱讀 40,926評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖恩脂,靈堂內(nèi)的尸體忽然破棺而出帽氓,到底是詐尸還是另有隱情,我是刑警寧澤俩块,帶...
    沈念sama閱讀 36,580評(píng)論 5 351
  • 正文 年R本政府宣布黎休,位于F島的核電站,受9級(jí)特大地震影響玉凯,放射性物質(zhì)發(fā)生泄漏势腮。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,259評(píng)論 3 336
  • 文/蒙蒙 一漫仆、第九天 我趴在偏房一處隱蔽的房頂上張望捎拯。 院中可真熱鬧,春花似錦盲厌、人聲如沸署照。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,750評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽藤树。三九已至,卻和暖如春拓萌,著一層夾襖步出監(jiān)牢的瞬間岁钓,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,867評(píng)論 1 274
  • 我被黑心中介騙來泰國(guó)打工微王, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留屡限,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 49,368評(píng)論 3 379
  • 正文 我出身青樓炕倘,卻偏偏與公主長(zhǎng)得像钧大,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子罩旋,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,930評(píng)論 2 361

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