vue-商品管理案例改進

来源:https://www.cnblogs.com/ygjzs/archive/2019/11/05/11802085.html
-Advertisement-
Play Games

案例改進 vue resource全局配置: 全局啟用 emulateJSON 選項 ...


案例改進

vue-resource全局配置:
Vue.http.options.root = 'http://vue.studyit.io/';
全局啟用 emulateJSON 選項
Vue.http.options.emulateJSON = true;

<div id="app">



    <div class="panel panel-primary">
      <div class="panel-heading">
        <h3 class="panel-title">添加品牌</h3>
      </div>
      <div class="panel-body form-inline">

        <label>
          Name:
          <input type="text" v-model="name" class="form-control">
        </label>

        <input type="button" value="添加" @click="add" class="btn btn-primary">
      </div>
    </div>



    <table class="table table-bordered table-hover table-striped">
      <thead>
        <tr>
          <th>Id</th>
          <th>Name</th>
          <th>Ctime</th>
          <th>Operation</th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="item in list" :key="item.id">
          <td>{{item.id}}</td>
          <td>{{item.name}}</td>
          <td>{{item.ctime}}</td>
          <td>
            <a href="" @click.prevent="del(item.id)">刪除</a>
          </td>
        </tr>
      </tbody>
    </table>
// 如果我們通過全局配置了,請求的數據介面 根功能變數名稱,則 ,在每次單獨發起 http 請求的時候,請求的 url 路徑,應該以相對路徑開頭,前面不能帶 /  ,否則 不會啟用根路徑做拼接;
    Vue.http.options.root = 'http://vue.studyit.io/';
    // 全局啟用 emulateJSON 選項
    Vue.http.options.emulateJSON = true;

    // 創建 Vue 實例,得到 ViewModel
    var vm = new Vue({
      el: '#app',
      data: {
        name: '',
        list: [ // 存放所有品牌列表的數組
        ]
      },
      created() { // 當 vm 實例 的 data 和 methods 初始化完畢後,vm實例會自動執行created 這個生命周期函數
        this.getAllList()
      },
      methods: {
        getAllList() { // 獲取所有的品牌列表 
          // 分析:
          // 1. 由於已經導入了 Vue-resource這個包,所以 ,可以直接通過  this.$http 來發起數據請求
          // 2. 根據介面API文檔,知道,獲取列表的時候,應該發起一個 get 請求
          // 3. this.$http.get('url').then(function(result){})
          // 4. 當通過 then 指定回調函數之後,在回調函數中,可以拿到數據伺服器返回的 result
          // 5. 先判斷 result.status 是否等於0,如果等於0,就成功了,可以 把 result.message 賦值給 this.list ; 如果不等於0,可以彈框提醒,獲取數據失敗!

          this.$http.get('api/getprodlist').then(result => {
            // 註意: 通過 $http 獲取到的數據,都在 result.body 中放著
            var result = result.body
            if (result.status === 0) {
              // 成功了
              this.list = result.message
            } else {
              // 失敗了
              alert('獲取數據失敗!')
            }
          })
        },
        add() {  // 添加品牌列表到後臺伺服器
          // 分析:
          // 1. 聽過查看 數據API介面,發現,要發送一個 Post 請求,  this.$http.post
          // 2. this.$http.post() 中接收三個參數:
          //   2.1 第一個參數: 要請求的URL地址
          //   2.2 第二個參數: 要提交給伺服器的數據 ,要以對象形式提交給伺服器 { name: this.name }
          //   3.3 第三個參數: 是一個配置對象,要以哪種表單數據類型提交過去, { emulateJSON: true }, 以普通表單格式,將數據提交給伺服器 application/x-www-form-urlencoded
          // 3. 在 post 方法中,使用 .then 來設置成功的回調函數,如果想要拿到成功的結果,需要 result.body

          /* this.$http.post('api/addproduct', { name: this.name }, { emulateJSON: true }).then(result => {
            if (result.body.status === 0) {
              // 成功了!
              // 添加完成後,只需要手動,再調用一下 getAllList 就能刷新品牌列表了
              this.getAllList()
              // 清空 name 
              this.name = ''
            } else {
              // 失敗了
              alert('添加失敗!')
            }
          }) */

          this.$http.post('api/addproduct', { name: this.name }).then(result => {
            if (result.body.status === 0) {
              // 成功了!
              // 添加完成後,只需要手動,再調用一下 getAllList 就能刷新品牌列表了
              this.getAllList()
              // 清空 name 
              this.name = ''
            } else {
              // 失敗了
              alert('添加失敗!')
            }
          })
        },
        del(id) { // 刪除品牌
          this.$http.get('api/delproduct/' + id).then(result => {
            if (result.body.status === 0) {
              // 刪除成功
              this.getAllList()
            } else {
              alert('刪除失敗!')
            }
          })
        }
      }
    });

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • private NotificationManager manager; private Notification.Builder builder; @Override protected void onCreate(Bundle savedInstanceState) { super.onCrea... ...
  • protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) fin... ...
  • private int year; private int monthOfYear; private int dayOfMonth; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(saved... ...
  • protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) fin... ...
  • @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (B ...
  • 實現列表動畫 ...
  • 鉤子函數實現小球彈落 ...
  • 不使用動畫 使用過渡類名實現動畫 修改vue的首碼 第三方類實現動畫 需引入css文件:`` ...
一周排行
    -Advertisement-
    Play Games
  • GoF之工廠模式 @目錄GoF之工廠模式每博一文案1. 簡單說明“23種設計模式”1.2 介紹工廠模式的三種形態1.3 簡單工廠模式(靜態工廠模式)1.3.1 簡單工廠模式的優缺點:1.4 工廠方法模式1.4.1 工廠方法模式的優缺點:1.5 抽象工廠模式1.6 抽象工廠模式的優缺點:2. 總結:3 ...
  • 新改進提供的Taurus Rpc 功能,可以簡化微服務間的調用,同時可以不用再手動輸出模塊名稱,或調用路徑,包括負載均衡,這一切,由框架實現並提供了。新的Taurus Rpc 功能,將使得服務間的調用,更加輕鬆、簡約、高效。 ...
  • 本章將和大家分享ES的數據同步方案和ES集群相關知識。廢話不多說,下麵我們直接進入主題。 一、ES數據同步 1、數據同步問題 Elasticsearch中的酒店數據來自於mysql資料庫,因此mysql數據發生改變時,Elasticsearch也必須跟著改變,這個就是Elasticsearch與my ...
  • 引言 在我們之前的文章中介紹過使用Bogus生成模擬測試數據,今天來講解一下功能更加強大自動生成測試數據的工具的庫"AutoFixture"。 什麼是AutoFixture? AutoFixture 是一個針對 .NET 的開源庫,旨在最大程度地減少單元測試中的“安排(Arrange)”階段,以提高 ...
  • 經過前面幾個部分學習,相信學過的同學已經能夠掌握 .NET Emit 這種中間語言,並能使得它來編寫一些應用,以提高程式的性能。隨著 IL 指令篇的結束,本系列也已經接近尾聲,在這接近結束的最後,會提供幾個可供直接使用的示例,以供大伙分析或使用在項目中。 ...
  • 當從不同來源導入Excel數據時,可能存在重覆的記錄。為了確保數據的準確性,通常需要刪除這些重覆的行。手動查找並刪除可能會非常耗費時間,而通過編程腳本則可以實現在短時間內處理大量數據。本文將提供一個使用C# 快速查找並刪除Excel重覆項的免費解決方案。 以下是實現步驟: 1. 首先安裝免費.NET ...
  • C++ 異常處理 C++ 異常處理機制允許程式在運行時處理錯誤或意外情況。它提供了捕獲和處理錯誤的一種結構化方式,使程式更加健壯和可靠。 異常處理的基本概念: 異常: 程式在運行時發生的錯誤或意外情況。 拋出異常: 使用 throw 關鍵字將異常傳遞給調用堆棧。 捕獲異常: 使用 try-catch ...
  • 優秀且經驗豐富的Java開發人員的特征之一是對API的廣泛瞭解,包括JDK和第三方庫。 我花了很多時間來學習API,尤其是在閱讀了Effective Java 3rd Edition之後 ,Joshua Bloch建議在Java 3rd Edition中使用現有的API進行開發,而不是為常見的東西編 ...
  • 框架 · 使用laravel框架,原因:tp的框架路由和orm沒有laravel好用 · 使用強制路由,方便介面多時,分多版本,分文件夾等操作 介面 · 介面開發註意欄位類型,欄位是int,查詢成功失敗都要返回int(對接java等強類型語言方便) · 查詢介面用GET、其他用POST 代碼 · 所 ...
  • 正文 下午找企業的人去鎮上做貸後。 車上聽同事跟那個司機對罵,火星子都快出來了。司機跟那同事更熟一些,連我在內一共就三個人,同事那一手指桑罵槐給我都聽愣了。司機也是老社會人了,馬上聽出來了,為那個無辜的企業經辦人辯護,實際上是為自己辯護。 “這個事情你不能怪企業。”“但他們總不能讓銀行的人全權負責, ...