欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

在Vue應(yīng)用中使用Netlify表單功能的方法詳解

Netlify 帶有內(nèi)置表單處理功能,可以用來(lái)存儲(chǔ)表單數(shù)據(jù),下載 csv 文件,同時(shí)可以在接收到新的提交時(shí)發(fā)送郵件通知或者通過(guò)配置 webhook 發(fā)送請(qǐng)求。

我們提供的服務(wù)有:成都做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)、微信公眾號(hào)開(kāi)發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、上街ssl等。為成百上千家企事業(yè)單位解決了網(wǎng)站和推廣的問(wèn)題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的上街網(wǎng)站制作公司

它是通過(guò)在部署應(yīng)用時(shí)直接解析 HTML 文件,識(shí)別 html 中的 form 標(biāo)簽來(lái)實(shí)現(xiàn)的,本文記錄如何在一個(gè) Vue 應(yīng)用中使用表單功能。

開(kāi)發(fā)

首先使用@vue/cli 新建一個(gè) Vue 應(yīng)用,完成一系列步驟后,運(yùn)行應(yīng)用

vue create my-awesome-app
...
yarn serve

創(chuàng)建一個(gè) form 表單

<!--
 data-netlify="true" 表明使用 form 功能
 netlify-honeypot="bot-field" 指定機(jī)器人識(shí)別字段
 -->
<template>
 <form
 id="app"
 method="POST"
 name="contact"
 data-netlify="true"
 netlify-honeypot="bot-field"
 @submit.prevent="handleSubmit"
 >
 <input name="bot-field" hidden>
 <label for="username">
  用戶名:
  <input
  type="text"
  id="username"
  placeholder="請(qǐng)輸入你的用戶名"
  name="username"
  v-model="form.username"
  >
 </label>
 <label for="email">
  郵箱:
  <input type="email" id="email" placeholder="請(qǐng)輸入你的郵箱" name="email" v-model="form.email">
 </label>
 <button type="submit">Submit</button>
 </form>
</template>

注意應(yīng)用的根節(jié)點(diǎn)一定要保留 id=''app" 屬性,否則經(jīng)過(guò)靜態(tài)化之后頁(yè)面上綁定的事件會(huì)失效

在 form 標(biāo)簽上監(jiān)聽(tīng) submit 事件,并且阻止瀏覽器默認(rèn)事件,使用 axios 提交 post 請(qǐng)求

yarn add axios

handleSubmit() {
 axios
 .post(
  "/",
  this.encode({
  "form-name": "contact", // 請(qǐng)求數(shù)據(jù)一定要加上 form-name 屬性
  ...this.form
  }),
  {
  header: { "Content-Type": "application/x-www-form-urlencoded" }
  }
 )
 .then(() => {
  alert("提交成功");
 })
 .catch(() => {
  alert("提交失敗");
 });
}

安裝預(yù)渲染插件 prerender-spa-plugin github.com/chrisvfritz… ,作用是靜態(tài)化 Vue 應(yīng)用,一定要預(yù)渲染 Vue 應(yīng)用,因?yàn)槿绻峭ㄟ^(guò) js 渲染的頁(yè)面, Netlify 是解析不到 form 表單的

yarn add prerender-spa-plugin --dev

新建一個(gè) vue.config.js 文件用來(lái)擴(kuò)展  webpack 配置

const path = require('path')
const PrerenderSPAPlugin = require('prerender-spa-plugin')

module.exports = {
 configureWebpack: () => {
 if (process.env.NODE_ENV !== 'production') return
 return {
  plugins: [
  new PrerenderSPAPlugin({
   staticDir: path.join(__dirname, 'dist'),
   routes: ['/']
  })
  ]
 }
 }
}

完整代碼如下

<template>
 <!--
 data-netlify="true" 表明使用 form 功能
 netlify-honeypot="bot-field" 指定機(jī)器人識(shí)別字段
 -->
 <form
 id="app"
 method="POST"
 name="contact"
 data-netlify="true"
 netlify-honeypot="bot-field"
 @submit.prevent="handleSubmit"
 >
 <input name="bot-field" hidden>
 <label for="username">
  用戶名:
  <input
  type="text"
  id="username"
  placeholder="請(qǐng)輸入你的用戶名"
  name="username"
  v-model="form.username"
  >
 </label>
 <label for="email">
  郵箱:
  <input type="email" id="email" placeholder="請(qǐng)輸入你的郵箱" name="email" v-model="form.email">
 </label>
 <button type="submit">Submit</button>
 </form>
</template>

<script>
import axios from "axios";

export default {
 name: "app",
 data() {
 return {
  form: {
  username: "",
  email: ""
  }
 };
 },
 methods: {
 encode(data) {
  return Object.keys(data)
  .map(
   key => `${encodeURIComponent(key)}=${encodeURIComponent(data[key])}`
  )
  .join("&");
 },
 handleSubmit() {
  axios
  .post(
   "/",
   this.encode({
   "form-name": "contact",
   ...this.form
   }),
   {
   header: { "Content-Type": "application/x-www-form-urlencoded" }
   }
  )
  .then(() => {
   alert("提交成功");
  })
  .catch(() => {
   alert("提交失敗");
  });
 }
 }
};
</script>

<style>
#app {
 font-family: "Avenir", Helvetica, Arial, sans-serif;
 -webkit-font-smoothing: antialiased;
 -moz-osx-font-smoothing: grayscale;
 text-align: center;
 color: #2c3e50;
 margin-top: 60px;
}
label {
 display: block;
}
</style>

部署

在 Github 上新建一個(gè)倉(cāng)庫(kù),上傳代碼,之后在 Netlify 上點(diǎn)擊 New site form Git,進(jìn)行授權(quán),完成授權(quán)后選擇要部署的項(xiàng)目倉(cāng)庫(kù)

在 Vue 應(yīng)用中使用 Netlify 表單功能的方法詳解 

 填寫(xiě)構(gòu)建命令,點(diǎn)擊 Deploy site 按鈕

在 Vue 應(yīng)用中使用 Netlify 表單功能的方法詳解 

 經(jīng)過(guò)一段時(shí)間的等待,不出意外應(yīng)用就部署成功了地址 

在 Vue 應(yīng)用中使用 Netlify 表單功能的方法詳解 

注意在提交數(shù)據(jù)中一定要有 form-name 屬性,否則 Netlify 會(huì)接收不到數(shù)據(jù),返回 404 錯(cuò)誤

在 Vue 應(yīng)用中使用 Netlify 表單功能的方法詳解 

 輸入測(cè)試數(shù)據(jù),點(diǎn)擊提交就可以在 Netlify 的站點(diǎn)操作面板看到數(shù)據(jù)了

 在 Vue 應(yīng)用中使用 Netlify 表單功能的方法詳解

總結(jié)

以上所述是小編給大家介紹的在 Vue 應(yīng)用中使用 Netlify 表單功能的方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)創(chuàng)新互聯(lián)網(wǎng)站的支持!

網(wǎng)站題目:在Vue應(yīng)用中使用Netlify表單功能的方法詳解
網(wǎng)頁(yè)URL:http://chinadenli.net/article8/gisoop.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、品牌網(wǎng)站建設(shè)網(wǎng)站維護(hù)、App設(shè)計(jì)、微信小程序網(wǎng)站策劃

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)