最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue中的CRUD使用及說明

 更新時(shí)間:2026年03月09日 09:08:12   作者:隨風(fēng)漂流6  
在Vue中進(jìn)行CRUD前端跨域操作時(shí),可以通過后端設(shè)置跨域來解決,通常有三種方法:使用`@CrossOrigin`注解、在`mvc`的XML配置文件中設(shè)置以及自定義類進(jìn)行配置,作者分享了這三種方法,并希望大家參考和使用

Vue中的CRUD

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title>bookinfo顯示</title>
			<link rel="stylesheet" href="css/bootstrap.min.css" rel="external nofollow" >
				<script src="js/jquery.min.js"></script>
				<script src="js/bootstrap.min.js"></script>
				<!--以上是引入bootstrap內(nèi)容-->
				<script src="js/axios.min.js"></script>
				<!--vue操作ajax的-->
				<script src="js/vue.js"></script>
				<!--引入vue-->
	</head>
	<style>
		img{
			width: 30px;
			height: 30px;
		}
	</style>
	<body>
		<div id="app">			
				<table class="table">
					<caption>用戶管理</caption>
					<thead>
						<tr>
							<th>編號(hào) <input type="text"  v-model="bookid"/></th>
							<th>書名<input type="text" v-model="bookname"/></th>
							<th>價(jià)格<input type="text" v-model="bookprice"/></th>
							<th>折扣價(jià)格<input type="text" v-model="discount"/></th>
							<th>圖片<input type="file" style="display: inline-block;"/></th>
							<th>類型<input type="text" v-model="btype"/></th>
							<td>
								<a href="" @click.prevent=" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" add()">添加一行</a>
							</td>
						</tr>	
						<tr class="active">
							<th>編號(hào)</th>
							<th>書名</th>
							<th>價(jià)格</th>
							<th>折扣價(jià)格</th>
							<th>圖片</th>
							<th>類型</th>
							<th>操作</th>
						</tr>
					</thead>
					<tbody>					
						<tr v-for="bookinfo in bookinfos" class="warning">
							<td>{{bookinfo.bookid}}</td>
							<td>{{bookinfo.bookname}}</td>
							<td>{{bookinfo.bookprice}}</td>
							<td>{{bookinfo.discount}}</td>
							<td><img v-bind:src="bookinfo.bookimg"/></td>
							<td>{{bookinfo.btype}}</td>
							<td>
								<a href="" @click.prevent=" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" del(bookinfo.bookid)">刪除</a>
								<a href="" @click.prevent=" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" update(bookinfo.bookid)">修改</a>
							</td>
						</tr>	
						<tr>
							<th>編號(hào) <input type="text" :disabled="true" v-model="bookid"/></th>
							<th>書名<input type="text" v-model="bookname"/></th>
							<th>價(jià)格<input type="text" v-model="bookprice"/></th>
							<th>折扣價(jià)格<input type="text" v-model="discount"/></th>
							<th>圖片<input type="file" style="display: inline-block;"/></th>
							<th>類型<input type="text" v-model="btype"/></th>
							<td>
								<a href="" @click.prevent=" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" updates()">確認(rèn)修改</a>
							</td>
						</tr>				
					</tbody>
				</table>
			</div>
		
	</body>
	<script>
		var v=new Vue({
			el:"#app",
			data:{
				res:"",
				bookid:"",
				bookname:"",
				bookprice:"",
				discount:"",
				btype:"",
				bookinfos:[]
			},
			mounted:function(){
				this.show();
			},
			methods:{
				show:function(){
					axios
					.get("http://localhost:8080/findAll")
					.then(function(response){
						v.bookinfos=response.data
					})
				},
				del:function(bookid){
					axios
					.get("http://localhost:8080/del/"+bookid)
					.then(function(response){
						if(response.data=="0"){
							v.bookinfos=v.bookinfos.filter(function(books){
								return books.bookid !=bookid
							})
						}else{
							alert("刪除失敗")
						}	
					})
					
				},
				update:function(bookid){
					var b=v.bookinfos.filter(function(books){
						return books.bookid ==bookid
					});
					v.bookid=b[0].bookid;
					v.bookname=b[0].bookname;
					v.bookprice=b[0].bookprice;
					v.discount=b[0].discount;
					v.btype=b[0].btype;
				},
				updates:function(){
					var params=new URLSearchParams();
					params.append("bookid",v.bookid);
					params.append("bookname",v.bookname);
					params.append("bookprice",v.bookprice);
					params.append("discount",v.discount);
					params.append("btype",v.btype);
					console.log(params)
					axios
					.post("http://localhost:8080/update",params)
					.then(function(response){
						console.log(response.data)
						if(response.data=="0"){
							v.bookinfos.some((books)=>{
								if(books.bookid==v.bookid){
									books.bookname=v.bookname;
									books.bookprice=v.bookprice;
									books.discount=v.discount;
									books.btype=v.btype;
									return true;
								}
							})
						}else{
							alert("修改失敗")
						}	
					})
					
					
					
				},
				add:function(){
					var param=new URLSearchParams();
					param.append("bookid",v.bookid);
					param.append("bookname",v.bookname);
					param.append("bookprice",v.bookprice);
					param.append("discount",v.discount);
					param.append("btype",v.btype);
					
					axios
					.post("http://localhost:8080/save",param)
					.then(function(response){
						if(response.data=="0"){
							var json = {bookid:v.bookid,bookname:v.bookname,bookprice:v.bookprice,discount:v.discount,btype:v.btype};
							v.bookinfos.push(json)
						}else{
							console.log("cuowu")
						}
					})
					
				}
			}
			
		})
		
		
		
	</script>
</html>

前端跨域問題

				axios
					.get("http://localhost:8080/del/"+bookid)
					.then(function(response){
						if(response.data=="0"){
							v.bookinfos=v.bookinfos.filter(function(books){
								return books.bookid !=bookid
							})
						}else{
							alert("刪除失敗")
						}	
					})

后端設(shè)置跨域 3種方式

方法1、@CrossOrigin

@Controller
@CrossOrigin   //設(shè)置了這個(gè)注解后就可以進(jìn)行跨域訪問
//@RestController 這個(gè)注解包含了 @Controller和 @ResponseBody
public class BookInFoController {
    @Autowired
    private BookInFoService bookInFoService;
    @ResponseBody
    @RequestMapping("/findAll")
    public List<BookInFo> findAll(){
        return bookInFoService.findAll();
    }

    @ResponseBody
    @RequestMapping("/del/{bookid}")
    public String del(@PathVariable("bookid") String bookid){
        if(bookInFoService.del(bookid)){
            return "0";
        }
        return "1";
    }
}

方法2.mvc的xml中配置

    <!-- API 接口跨域配置 -->
    <mvc:cors>-->
        <mvc:mapping path="/**" allowed-origins="*"
                     allowed-methods="POST, GET, OPTIONS, DELETE, PUT"
                     allowed-headers="Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With"                    allow-credentials="true" max-age="3600" />//是否允許跨域,最大連接時(shí)間為1小時(shí)
    </mvc:cors>-->

方法3.自定義類進(jìn)行配置

package com.tellhow.booksystem.util;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
//import org.springframework.web.cors.CorsConfiguration;
//import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
//import org.springframework.web.filter.CorsFilter;

import com.sun.jndi.url.dns.dnsURLContext;

/**
 *
 *
 * 處理跨域請(qǐng)求的過濾器
 */
@Configuration
public class GlobalCorsConfig {

//    @Bean
//    public CorsFilter corsFilter() {
//
//        //1.添加CORS配置信息
//        CorsConfiguration config = new CorsConfiguration();
//
//        //1) 允許的域,不要寫*,否則cookie就無法使用了-------------在此處,配置允許跨域的請(qǐng)求
//        //http://localhost:63342/
//        config.addAllowedOrigin("http://manage.shopping.com");
//        config.addAllowedOrigin("http://api.shopping.com");
//        config.addAllowedOrigin("http://img.shopping.com");
//        config.addAllowedOrigin("http://www.shopping.com");
//        //2) 是否發(fā)送Cookie信息
//        config.setAllowCredentials(true);  //該配置表示, 允許跨域時(shí),傳遞cookie
//        //3) 允許的請(qǐng)求方式
//        config.addAllowedMethod("OPTIONS");
//        config.addAllowedMethod("HEAD");
//        config.addAllowedMethod("GET");
//        config.addAllowedMethod("PUT");
//        config.addAllowedMethod("POST");
//        config.addAllowedMethod("DELETE");
//        config.addAllowedMethod("PATCH");
//        // 4)允許的頭信息
//        config.addAllowedHeader("*");
//
//        //5、允許時(shí)間
//        config.setMaxAge(3600L);//3600秒有效
//
//        //2.添加映射路徑,我們攔截一切請(qǐng)求
//        UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
//        configSource.registerCorsConfiguration("/**", config);
//
//        //3.返回新的CorsFilter.
//        return new CorsFilter(configSource);
//    }
}

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • vue引入高德地圖并繪制點(diǎn)線面的方法

    vue引入高德地圖并繪制點(diǎn)線面的方法

    這篇文章主要介紹了vue引入高德地圖并繪制點(diǎn)線面的實(shí)例代碼,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧
    2024-03-03
  • Vue3使用mitt進(jìn)行組件通信的步驟

    Vue3使用mitt進(jìn)行組件通信的步驟

    這篇文章主要介紹了Vue3使用mitt進(jìn)行組件通信的步驟,幫助大家更好的理解和學(xué)習(xí)使用vue,感興趣的朋友可以了解下
    2021-05-05
  • vue中el-dialog打開與關(guān)閉的幾種方式

    vue中el-dialog打開與關(guān)閉的幾種方式

    本文主要介紹了vue中el-dialog打開與關(guān)閉的幾種方式,包括 update:visible, ref和兄弟 bus這三種方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-01-01
  • 使用Vue3+PDF.js實(shí)現(xiàn)PDF預(yù)覽功能

    使用Vue3+PDF.js實(shí)現(xiàn)PDF預(yù)覽功能

    項(xiàng)目中有一個(gè)需要預(yù)覽下載pdf的需求,網(wǎng)上找了很久,決定使用 pdf.js 完成,下面這篇文章主要給大家介紹了關(guān)于使用Vue3+PDF.js實(shí)現(xiàn)PDF預(yù)覽功能的相關(guān)資料,需要的朋友可以參考下
    2022-12-12
  • vue封裝TabBar組件的完整步驟記錄

    vue封裝TabBar組件的完整步驟記錄

    組件封裝是為了復(fù)用,換成大白話就是,同樣的事情我不想做第二遍,節(jié)省出來的時(shí)間用來看動(dòng)漫不香嗎,下面這篇文章主要給大家介紹了關(guān)于vue封裝TabBar組件的完整步驟,需要的朋友可以參考下
    2021-10-10
  • 手寫Vue源碼之?dāng)?shù)據(jù)劫持示例詳解

    手寫Vue源碼之?dāng)?shù)據(jù)劫持示例詳解

    這篇文章主要給大家介紹了手寫Vue源碼之?dāng)?shù)據(jù)劫持的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • Vue3(ts)使用vee-validate表單校驗(yàn),自定義全局驗(yàn)證規(guī)則說明

    Vue3(ts)使用vee-validate表單校驗(yàn),自定義全局驗(yàn)證規(guī)則說明

    這篇文章主要介紹了Vue3(ts)使用vee-validate表單校驗(yàn),自定義全局驗(yàn)證規(guī)則說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • 如何在Vue項(xiàng)目中使用vuex

    如何在Vue項(xiàng)目中使用vuex

    這篇文章主要介紹了如何在Vue項(xiàng)目中使用vuex問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • 詳解vue身份認(rèn)證管理和租戶管理

    詳解vue身份認(rèn)證管理和租戶管理

    本篇開始功能模塊的開發(fā),首先完成ABP模板自帶的身份認(rèn)證管理模塊和租戶管理模塊。同樣的,參考ABP的Angular版本來做。
    2021-05-05
  • vue3將頁面生成pdf導(dǎo)出的操作指南

    vue3將頁面生成pdf導(dǎo)出的操作指南

    最近工作中有需要將一些前端頁面(如報(bào)表頁面等)導(dǎo)出為pdf的需求,下面這篇文章主要給大家介紹了關(guān)于vue3 如何將頁面生成 pdf 導(dǎo)出,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-07-07

最新評(píng)論

仙桃市| 梁平县| 万源市| 西乌| 汪清县| 绩溪县| 台中市| 监利县| 章丘市| 遵义市| 乌兰察布市| 邓州市| 长乐市| 肇庆市| 鹤岗市| 宁德市| 隆安县| 原平市| 上犹县| 渝北区| 华坪县| 尼勒克县| 耒阳市| 伊金霍洛旗| 西峡县| 惠东县| 中方县| 都匀市| 固原市| 固镇县| 定安县| 宝鸡市| 惠水县| 神农架林区| 吴桥县| 黄大仙区| 西贡区| 辽阳市| 苏尼特右旗| 武夷山市| 普兰店市|