Options API(中小規模PJ向け)
<script>
export default {
data() {
return {
message: 'Hello World!'
}
}
}
</script><script>
export default {
data() {
return {
message: 'Hello World!'
}
},
methods: {
reverseMessage() {
this.message = this.message.split('').reverse().join('')
},
notify() {
alert('navigation was prevented.')
}
}
}
</script><script>
export default {
data() {
return {
message: 'Hello World!',
isRed: true,
color: 'green'
}
},
methods: {
toggleRed() {
this.isRed = !this.isRed
},
toggleColor() {
this.color = this.color === 'green' ? 'blue' : 'green'
}
}
}
</script><script>
import TodoItem from './TodoItem.vue'
export default {
components: {
TodoItem
},
data() {
return {
groceryList: [
{ id: 0, text: 'Vegetables' },
{ id: 1, text: 'Cheese' },
{ id: 2, text: 'Whatever else humans are supposed to eat' }
]
}
}
}
</script><script>
import { marked } from 'marked'
import { debounce } from 'lodash-es'
export default {
data: () => ({
input: '# hello'
}),
computed: {
output() {
return marked(this.input)
}
},
methods: {
update: debounce(function (e) {
this.input = e.target.value
}, 100)
}
}
</script><script>
const API_URL = `https://api.github.com/repos/vuejs/core/commits?per_page=3&sha=`
export default {
data: () => ({
branches: ['main', 'v2-compat'],
currentBranch: 'main',
commits: null
}),
created() {
// fetch on init
this.fetchData()
},
watch: {
// re-fetch whenever currentBranch changes
currentBranch: 'fetchData'
},
methods: {
async fetchData() {
const url = `${API_URL}${this.currentBranch}`
this.commits = await (await fetch(url)).json()
},
truncate(v) {
const newline = v.indexOf('\n')
return newline > 0 ? v.slice(0, newline) : v
},
formatDate(v) {
return v.replace(/T|Z/g, ' ')
}
}
}
</script><script>
import PolyGraph from './PolyGraph.vue'
export default {
components: {
PolyGraph
},
data: () => ({
newLabel: '',
stats: [
{ label: 'A', value: 100 },
{ label: 'B', value: 100 },
{ label: 'C', value: 100 },
{ label: 'D', value: 100 },
{ label: 'E', value: 100 },
{ label: 'F', value: 100 }
]
}),
methods: {
add(e) {
e.preventDefault()
if (!this.newLabel) return
this.stats.push({
label: this.newLabel,
value: 100
})
this.newLabel = ''
},
remove(stat) {
if (this.stats.length > 3) {
this.stats.splice(this.stats.indexOf(stat), 1)
} else {
alert("Can't delete more!")
}
}
}
}
</script><script>
export default {
data() {
return {
duration: 15 * 1000,
elapsed: 0
}
},
created() {
this.reset()
},
unmounted() {
cancelAnimationFrame(this.handle)
},
computed: {
progressRate() {
return Math.min(this.elapsed / this.duration, 1)
}
},
methods: {
update() {
this.elapsed = performance.now() - this.lastTime
if (this.elapsed >= this.duration) {
cancelAnimationFrame(this.handle)
} else {
this.handle = requestAnimationFrame(this.update)
}
},
reset() {
this.elapsed = 0
this.lastTime = performance.now()
this.update()
}
}
}
</script>Composition API(大規模PJ向け)
SFCではsetup()ではなく<script setup>を強く推奨
<script setup>
import { ref } from 'vue'
const message = ref('Hello World!')
</script>
<script setup>
import { ref } from 'vue'
const message = ref('Hello World!')
function reverseMessage() {
message.value = message.value.split('').reverse().join('')
}
function notify() {
alert('navigation was prevented.')
}
</script>
<script setup>
import { ref } from 'vue'
const message = ref('Hello World!')
const isRed = ref(true)
const color = ref('green')
function toggleRed() {
isRed.value = !isRed.value
}
function toggleColor() {
color.value = color.value === 'green' ? 'blue' : 'green'
}
</script>
<script setup>
import { ref } from 'vue'
import TodoItem from './TodoItem.vue'
const groceryList = ref([
{ id: 0, text: 'Vegetables' },
{ id: 1, text: 'Cheese' },
{ id: 2, text: 'Whatever else humans are supposed to eat' }
])
</script>
<script setup>
import { marked } from 'marked'
import { debounce } from 'lodash-es'
import { ref, computed } from 'vue'
const input = ref('# hello')
const output = computed(() => marked(input.value))
const update = debounce((e) => {
input.value = e.target.value
}, 100)
</script>
<script setup>
import { ref, watchEffect } from 'vue'
const API_URL = `https://api.github.com/repos/vuejs/core/commits?per_page=3&sha=`
const branches = ['main', 'v2-compat']
const currentBranch = ref(branches[0])
const commits = ref(null)
watchEffect(async () => {
// this effect will run immediately and then
// re-run whenever currentBranch.value changes
const url = `${API_URL}${currentBranch.value}`
commits.value = await (await fetch(url)).json()
})
function truncate(v) {
const newline = v.indexOf('\n')
return newline > 0 ? v.slice(0, newline) : v
}
function formatDate(v) {
return v.replace(/T|Z/g, ' ')
}
</script>
<script setup>
import PolyGraph from './PolyGraph.vue'
import { ref, reactive } from 'vue'
const newLabel = ref('')
const stats = reactive([
{ label: 'A', value: 100 },
{ label: 'B', value: 100 },
{ label: 'C', value: 100 },
{ label: 'D', value: 100 },
{ label: 'E', value: 100 },
{ label: 'F', value: 100 }
])
function add(e) {
e.preventDefault()
if (!newLabel.value) return
stats.push({
label: newLabel.value,
value: 100
})
newLabel.value = ''
}
function remove(stat) {
if (stats.length > 3) {
stats.splice(stats.indexOf(stat), 1)
} else {
alert("Can't delete more!")
}
}
</script>
<script setup>
import { ref, onUnmounted, computed } from 'vue'
const duration = ref(15 * 1000)
const elapsed = ref(0)
let lastTime
let handle
const update = () => {
elapsed.value = performance.now() - lastTime
if (elapsed.value >= duration.value) {
cancelAnimationFrame(handle)
} else {
handle = requestAnimationFrame(update)
}
}
const reset = () => {
elapsed.value = 0
lastTime = performance.now()
update()
}
const progressRate = computed(() =>
Math.min(elapsed.value / duration.value, 1)
)
reset()
onUnmounted(() => {
cancelAnimationFrame(handle)
})
</script>
出典:https://ja.vuejs.org/examples/#hello-world
#Composition API #Options API #Vue #Vue.js