<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
</head>
<body>
<div id="app">
<template>
<el-select
ref="select"
v-model="value"
filterable
allow-create
default-first-option
@keyup.enter.native="change"
placeholder="请选择文章标签">
<el-option
v-for="item in options"
:key="item.value"
:label="item.label"
:value="item.value">
</el-option>
</el-select>
</template>
</div>
<script>
var app = new Vue({
el: '#app',
data () {
return {
// apiArray: [{ // 接口返回的选项内容
// value: 'HTML',
// label: 'HTML'
// }, {
// value: 'CSS',
// label: 'CSS'
// }, {
// value: 'JavaScript',
// label: 'JavaScript'
// }],
value: [],
addArray: [], // 输入的新数组
options: [] // 页面调用的select数组
}
},
created() {
this.options = localStorage.getItem('options') === null ? [] : JSON.parse(localStorage.getItem('options'))
},
methods: {
change() {
this.addArray.push({
value: this.$refs.select.createdLabel,
label: this.$refs.select.createdLabel
})
this.options = this.addArray
localStorage.setItem('options', JSON.stringify(this.options))
}
}
})
</script>
</body>
</html>