<script type="text/javascript"> // new 一个 vue实例 new Vue({ el: "#app", data: { // 定义一个 数组 去接受后端传过来的数据 bookList:[], }, // 方法都写在这里面 methods: { // 查询所有书籍 selectAllBook(){ const _this = this // 在这里发送请求,获取到后端的数据 $.ajax({ // 设置请求路径 url:'/book', // 设置请求方式 type:'get', // 数据:传给后端的数据 data:{ method:"selectAllBook", }, // 设置返回值类型 dataType:'json', // 设置成功的回调函数 success:function (rs) { // rs 是一个参数,接收的后端的返回数据 _this.booList = rs.data // console.log(rs); } }) } }, mounted(){ this.selectAllBook(); }, }) </script>
时间: 2024-02-10 07:20:28 浏览: 244
这段代码是使用 Vue.js 框架开发的一个前端页面,包含了一个 Vue 实例和一个方法 selectAllBook()。这个方法会发送一个 AJAX 请求到后端接口,获取所有书籍的数据并存储在 bookList 数组中。代码中使用了 jQuery 库来发送 AJAX 请求。另外,mounted() 方法会在 Vue 实例挂载后自动调用 selectAllBook() 方法,从而自动获取并显示所有书籍的数据。
相关问题
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="vue/vue.js"></script> </head> <body> <div id="app"> <input type="text" v-model="message"> {{message}} </div> <div id="v1"> <input type="text" v-model=""> </div> </body> <script> new Vue({ el: "#app", data:{ message: "Hello Vue" } }) new Vue1({ el: "#v1", data:{ message: "123" } }) </script> </html> vue1为什么不运行
在你的 HTML 代码中,你只引入了 Vue.js 一次,而在你的 JavaScript 代码中,你却创建了两个 Vue 实例,一个是 `new Vue`,另一个是 `new Vue1`。因此,当浏览器处理到 `new Vue1` 时,会抛出一个错误,因为 Vue1 并没有被定义过。
如果你想要让 `new Vue1` 正常运行,你需要在 HTML 代码中引入一个新的 Vue.js 文件,或者复制并重命名 Vue.js 文件,然后在 HTML 代码中引入它,例如:
```
<script src="vue/vue.js"></script>
<script src="vue/vue1.js"></script>
```
然后在 JavaScript 代码中创建一个新的 Vue 实例:
```
new Vue1({
el: "#v1",
data: {
message: "123"
}
})
```
这样就可以让 `new Vue1` 运行了。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script src="js/vue.min.js"></script> </head> <body> <div id="app"> <a v-bind:href="url">{{text}}</a><br> <a :href="url">{{text}}2</a> </div> </body> <script> new new Vue({ el: "#app", data: { url: "D:/JavaTest/web/vue/01_vue_%E5%BF%AB%E9%80%9F%E5%85%A5%E9%97%A8.html", text: "快速入门" } }) </script> </html> 执行报错 Uncaught TypeError: (intermediate value) is not a constructor
这个错误是因为在创建Vue实例时存在一个多余的"new"关键字。在代码中,你意外地在"new Vue"之前多写了一个"new"关键字。请将代码中的第10行改为以下形式:
```javascript
new Vue({
el: "#app",
data: {
url: "D:/JavaTest/web/vue/01_vue_%E5%BF%AB%E9%80%9F%E5%85%A5%E9%97%A8.html",
text: "快速入门"
}
})
```
这样就可以修复这个错误了。注意,我还将Vue.js库的引用路径修改为了相对路径(js/vue.min.js),以便在本地运行时能够找到该文件。你也可以根据需要调整路径。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20250102104920.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)
0">
{{bk.bookName}} ¥{{bk.bookPrice}} //列表页——分页 layui.use(['laypage'], function () { var laypage = layui.laypage; laypage.render({ elem: 'houseList' , count: 25 , limit: 5 , theme: '#daba91' , layout: ['page', 'next'] }); }); Vue.config.productionTip = false //阻止vue在启动时生成生产提示 new Vue({ el: "#app", data() { return { bookAll: [] } }, mounted() { var _this = this; axios({ method: 'get', url: 'http://localhost:8080/BookShopSystem_war/getBookAll' }).then(function (result) { var code = result.data.code; var data = result.data.data; if (code == 0) { _this.bookAll = data; } console.log(data) }) } })已省略部分代码,数据都可遍历出来,如何实现分页功能
![](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)