computed: { titleCount() { return this.title.length }, contentCount() { return this.content.length }, },
时间: 2024-05-17 10:15:18 浏览: 17
这段代码是 Vue.js 中 `computed` 属性的定义,其中定义了两个方法 `titleCount` 和 `contentCount`。这两个方法分别返回当前对象的 `title` 和 `content` 字段的长度,用于在 Vue 模板中动态显示输入框中已输入文本的长度。具体来说,当用户在输入框中输入或删除字符时,这两个方法会自动重新计算并更新对应的计算属性值,并触发相应的视图更新。这样,用户就可以随时了解自己已经输入了多少字符,以便更好地控制输入内容的长度和格式。
相关问题
axios.defaults.baseURL = 'http://localhost:8080/book/'; var vm = new Vue({ el: '#app', data: { id: '', bookName:'', flag: false, submitFlag: false, books: [] }, methods:{ handle: async function () { //编辑 var book = { id: this.id, bookName: this.bookName }; await axios.put(book.id, book); await this.queryBooks(); this.id = ''; this.bookName = ''; this.flag = false; }, //编辑处理 toEdit: async function (id) { var response = await axios.get(id); this.id = response.data.id; this.bookName = response.data.bookName; this.flag = true; }, deleteBook: async function (id) { }, queryBooks: async function () { var response = await axios.get(); this.books = response.data; } }, computed:{ total: function () { return this.books.length; } }, watch: { name: function (val) { console.log(val); } }, mounted:function () { this.queryBooks(); } });补全代码
这段代码已经是一个完整的 Vue.js 组件,如果你想在页面中使用这个组件,需要在 HTML 中引入 Vue.js 库,并在页面中添加一个元素作为 Vue.js 组件的挂载点。示例代码如下:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Vue.js 组件</title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
</head>
<body>
<div id="app">
<table>
<thead>
<tr>
<th>ID</th>
<th>书名</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr v-for="(book, index) in books" :key="index">
<td>{{ book.id }}</td>
<td>{{ book.bookName }}</td>
<td>
<button @click="toEdit(book.id)">编辑</button>
<button @click="deleteBook(book.id)">删除</button>
</td>
</tr>
</tbody>
</table>
<div v-if="flag">
<input type="text" v-model="bookName">
<button @click="handle">保存</button>
</div>
</div>
<script>
var vm = new Vue({
el: '#app',
data: {
id: '',
bookName: '',
flag: false,
submitFlag: false,
books: []
},
methods: {
handle: async function () {
// 编辑
var book = {
id: this.id,
bookName: this.bookName
};
await axios.put(book.id, book);
await this.queryBooks();
this.id = '';
this.bookName = '';
this.flag = false;
},
// 编辑处理
toEdit: async function (id) {
var response = await axios.get(id);
this.id = response.data.id;
this.bookName = response.data.bookName;
this.flag = true;
},
deleteBook: async function (id) {
// 删除
if (confirm('确定删除该书吗?')) {
await axios.delete(id);
await this.queryBooks();
}
},
queryBooks: async function () {
var response = await axios.get();
this.books = response.data;
}
},
computed: {
total: function () {
return this.books.length;
}
},
watch: {
name: function (val) {
console.log(val);
}
},
mounted: function () {
this.queryBooks();
}
});
</script>
</body>
</html>
```
在上面的示例代码中,我们使用了 Vue.js 的模板语法来渲染表格和表单,使用了 `v-for` 指令来循环渲染每一行数据,使用了 `v-if` 指令来判断表单是否需要显示。在 Vue.js 组件中,我们可以使用 `data` 属性来定义组件的数据,使用 `methods` 属性来定义组件的方法,使用 `computed` 属性来定义组件的计算属性,使用 `watch` 属性来监听组件的数据变化,使用 `mounted` 生命周期钩子来在组件挂载后执行一些操作。
转换vue3 写法 data () { return { title: '市南区', value: 9323, topData: [], filterData: [], navTop: [], // 名字行政区划 xsource: [], // 值 centerWidth: '', // 外面柱状图的 show: true, centerHeight: '' } }, computed: { defaultOption () { return { step: 0.5 // 数值越大速度滚动越快 } } }, props: { option: { type: Object, default: () => { return [] } } }, created () { this.$ref.scroll3._startMove() }, destroyed () { this.$refs.scroll3._cancle() }, watch: { option: { immediate: true, deep: true, handler: function (newValue, oldValue) { this.show = true if (!newValue.基础配置.swiper) { this.show = false } this.filterData = newValue.数据.source this.navTop = newValue.数据配置.xDimensions // name 行政区划 this.xsource = newValue.数据配置.yDimensions this.centerWidth = newValue.基础配置.centerWidth this.centerHeight = newValue.基础配置.centerHeight this.topHeight = newValue.基础配置.topHeight let maxValue = 0 this.filterData.forEach((item, index) => { if (Number(item[this.xsource[0].name]) > maxValue) { maxValue = item[this.xsource[0].name] } }) this.filterData.forEach((item, index) => { if (item[this.xsource[0].name] == maxValue) { item.width = 100 } else { item.width = item[this.xsource[0].name] / maxValue * 100 } if (newValue.基础配置.leftBackground) { item.leftBackground = newValue.基础配置.leftBackground } else { item.leftBackground = 'linear-gradient(90deg, #2F61C2 0%, #60C0E8 100%)' } }) // 最大的值 let max = [] for (var i = 0; i < this.filterData.length; i++) { for (var j = i; j < this.filterData.length; j++) { if (Number(this.filterData[i][this.xsource[0].name]) < Number(this.filterData[j][this.xsource[0].name])) { max = this.filterData[j] this.filterData[j] = this.filterData[i] this.filterData[i] = max } } } } } }
在Vue3中,可以将data、computed、props、created、destroyed和watch等属性直接写在setup函数中,代码如下:
```
import { ref, watch } from 'vue'
export default {
setup(props) {
const title = ref('市南区')
const value = ref(9323)
const topData = ref([])
const filterData = ref([])
const navTop = ref([])
const xsource = ref([])
const centerWidth = ref('')
const show = ref(true)
const centerHeight = ref('')
const topHeight = ref('')
const defaultOption = ref({
step: 0.5
})
const startMove = () => {
// TODO
}
const cancle = () => {
// TODO
}
watch(props.option, (newValue, oldValue) => {
show.value = true
if (!newValue.基础配置.swiper) {
show.value = false
}
filterData.value = newValue.数据.source
navTop.value = newValue.数据配置.xDimensions // name 行政区划
xsource.value = newValue.数据配置.yDimensions
centerWidth.value = newValue.基础配置.centerWidth
centerHeight.value = newValue.基础配置.centerHeight
topHeight.value = newValue.基础配置.topHeight
let maxValue = 0
filterData.value.forEach((item, index) => {
if (Number(item[xsource.value[0].name]) > maxValue) {
maxValue = item[xsource.value[0].name]
}
})
filterData.value.forEach((item, index) => {
if (item[xsource.value[0].name] == maxValue) {
item.width = 100
} else {
item.width = item[xsource.value[0].name] / maxValue * 100
}
if (newValue.基础配置.leftBackground) {
item.leftBackground = newValue.基础配置.leftBackground
} else {
item.leftBackground = 'linear-gradient(90deg, #2F61C2 0%, #60C0E8 100%)'
}
})
// 最大的值
let max = []
for (var i = 0; i < filterData.value.length; i++) {
for (var j = i; j < filterData.value.length; j++) {
if (Number(filterData.value[i][xsource.value[0].name]) < Number(filterData.value[j][xsource.value[0].name])) {
max = filterData.value[j]
filterData.value[j] = filterData.value[i]
filterData.value[i] = max
}
}
}
}, { immediate: true, deep: true })
return {
title,
value,
topData,
filterData,
navTop,
xsource,
centerWidth,
show,
centerHeight,
topHeight,
defaultOption,
startMove,
cancle
}
}
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)