docs(Tabbar): use composition api

This commit is contained in:
chenjiahan 2020-12-13 14:15:36 +08:00
parent af49575484
commit f9b6fda65c
4 changed files with 121 additions and 89 deletions

View File

@ -25,11 +25,12 @@ app.use(TabbarItem);
``` ```
```js ```js
import { ref } from 'vue';
export default { export default {
data() { setup() {
return { const active = ref(0);
active: 0, return { active };
};
}, },
}; };
``` ```
@ -46,11 +47,12 @@ export default {
``` ```
```js ```js
import { ref } from 'vue';
export default { export default {
data() { setup() {
return { const active = ref('home');
active: 'home', return { active };
};
}, },
}; };
``` ```
@ -84,14 +86,18 @@ Use `icon` slot to custom icon.
``` ```
```js ```js
import { ref } from 'vue';
export default { export default {
data() { setup() {
return { const active = ref(0);
active: 0, const icon = {
icon: {
active: 'https://img.yzcdn.cn/vant/user-active.png', active: 'https://img.yzcdn.cn/vant/user-active.png',
inactive: 'https://img.yzcdn.cn/vant/user-inactive.png', inactive: 'https://img.yzcdn.cn/vant/user-inactive.png',
}, };
return {
icon,
active,
}; };
}, },
}; };
@ -120,13 +126,20 @@ export default {
``` ```
```js ```js
import { Notify } from 'vant'; import { ref } from 'vue';
import { Toast } from 'vant';
export default { export default {
methods: { setup() {
onChange(index) { const active = ref(0);
Notify({ type: 'primary', message: index }); const onChange = (index) => {
}, Toast(`Tab ${index}`);
};
return {
icon,
onChange,
};
}, },
}; };
``` ```

View File

@ -27,11 +27,12 @@ app.use(TabbarItem);
``` ```
```js ```js
import { ref } from 'vue';
export default { export default {
data() { setup() {
return { const active = ref(0);
active: 0, return { active };
};
}, },
}; };
``` ```
@ -50,11 +51,12 @@ export default {
``` ```
```js ```js
import { ref } from 'vue';
export default { export default {
data() { setup() {
return { const active = ref('home');
active: 'home', return { active };
};
}, },
}; };
``` ```
@ -90,14 +92,18 @@ export default {
``` ```
```js ```js
import { ref } from 'vue';
export default { export default {
data() { setup() {
return { const active = ref(0);
active: 0, const icon = {
icon: {
active: 'https://img.yzcdn.cn/vant/user-active.png', active: 'https://img.yzcdn.cn/vant/user-active.png',
inactive: 'https://img.yzcdn.cn/vant/user-inactive.png', inactive: 'https://img.yzcdn.cn/vant/user-inactive.png',
}, };
return {
icon,
active,
}; };
}, },
}; };
@ -126,13 +132,20 @@ export default {
``` ```
```js ```js
import { Notify } from 'vant'; import { ref } from 'vue';
import { Toast } from 'vant';
export default { export default {
methods: { setup() {
onChange(index) { const active = ref(0);
Notify({ type: 'primary', message: index }); const onChange = (index) => {
}, Toast(`标签 ${index}`);
};
return {
icon,
onChange,
};
}, },
}; };
``` ```

View File

@ -62,17 +62,20 @@
<demo-block :title="t('switchEvent')"> <demo-block :title="t('switchEvent')">
<van-tabbar v-model="active5" @change="onChange"> <van-tabbar v-model="active5" @change="onChange">
<van-tabbar-item icon="home-o">{{ t('tab') + 1 }}</van-tabbar-item> <van-tabbar-item icon="home-o">{{ `${t('tab')} 1` }}</van-tabbar-item>
<van-tabbar-item icon="search">{{ t('tab') + 2 }}</van-tabbar-item> <van-tabbar-item icon="search">{{ `${t('tab')} 2` }}</van-tabbar-item>
<van-tabbar-item icon="friends-o">{{ t('tab') + 3 }}</van-tabbar-item> <van-tabbar-item icon="friends-o">{{ `${t('tab')} 3` }}</van-tabbar-item>
<van-tabbar-item icon="setting-o">{{ t('tab') + 4 }}</van-tabbar-item> <van-tabbar-item icon="setting-o">{{ `${t('tab')} 4` }}</van-tabbar-item>
</van-tabbar> </van-tabbar>
</demo-block> </demo-block>
</template> </template>
<script> <script lang="ts">
export default { import { reactive, toRefs } from 'vue';
i18n: { import { useTranslate } from '@demo/use-translate';
import Toast from '../../toast';
const i18n = {
'zh-CN': { 'zh-CN': {
badge: '徽标提示', badge: '徽标提示',
customIcon: '自定义图标', customIcon: '自定义图标',
@ -89,31 +92,34 @@ export default {
switchEvent: 'Change Event', switchEvent: 'Change Event',
selectTip: 'You select ', selectTip: 'You select ',
}, },
}, };
data() { export default {
return { setup() {
const t = useTranslate(i18n);
const state = reactive({
active: 0, active: 0,
active2: 0, active2: 0,
active3: 0, active3: 0,
active4: 0, active4: 0,
active5: 0, active5: 0,
activeName: 'home', activeName: 'home',
});
const onChange = (index: number) => {
Toast(`${t('tab')} ${index + 1}`);
};
return {
...toRefs(state),
t,
icon: { icon: {
active: 'https://img.yzcdn.cn/vant/user-active.png', active: 'https://img.yzcdn.cn/vant/user-active.png',
inactive: 'https://img.yzcdn.cn/vant/user-inactive.png', inactive: 'https://img.yzcdn.cn/vant/user-inactive.png',
}, },
onChange,
}; };
}, },
methods: {
onChange(index) {
this.$notify({
type: 'primary',
message: `${this.t('selectTip')} ${this.t('tab')}${index + 1}`,
});
},
},
}; };
</script> </script>