Netlify 帶有內(nèi)置表單處理功能,可以用來存儲表單數(shù)據(jù),下載 csv 文件,同時可以在接收到新的提交時發(fā)送郵件通知或者通過配置 webhook 發(fā)送請求。
我們提供的服務(wù)有:成都做網(wǎng)站、成都網(wǎng)站設(shè)計、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、上街ssl等。為成百上千家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的上街網(wǎng)站制作公司
它是通過在部署應(yīng)用時直接解析 HTML 文件,識別 html 中的 form 標(biāo)簽來實現(xiàn)的,本文記錄如何在一個 Vue 應(yīng)用中使用表單功能。
開發(fā)
首先使用@vue/cli 新建一個 Vue 應(yīng)用,完成一系列步驟后,運行應(yīng)用
vue create my-awesome-app ... yarn serve
創(chuàng)建一個 form 表單
<!-- data-netlify="true" 表明使用 form 功能 netlify-honeypot="bot-field" 指定機器人識別字段 --> <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="請輸入你的用戶名" name="username" v-model="form.username" > </label> <label for="email"> 郵箱: <input type="email" id="email" placeholder="請輸入你的郵箱" name="email" v-model="form.email"> </label> <button type="submit">Submit</button> </form> </template>
注意應(yīng)用的根節(jié)點一定要保留 id=''app" 屬性,否則經(jīng)過靜態(tài)化之后頁面上綁定的事件會失效
在 form 標(biāo)簽上監(jiān)聽 submit 事件,并且阻止瀏覽器默認(rèn)事件,使用 axios 提交 post 請求
yarn add axios
handleSubmit() {
axios
.post(
"/",
this.encode({
"form-name": "contact", // 請求數(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)用,因為如果是通過 js 渲染的頁面, Netlify 是解析不到 form 表單的
yarn add prerender-spa-plugin --dev
新建一個 vue.config.js 文件用來擴展 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" 指定機器人識別字段
-->
<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="請輸入你的用戶名"
name="username"
v-model="form.username"
>
</label>
<label for="email">
郵箱:
<input type="email" id="email" placeholder="請輸入你的郵箱" 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 上新建一個倉庫,上傳代碼,之后在 Netlify 上點擊 New site form Git,進(jìn)行授權(quán),完成授權(quán)后選擇要部署的項目倉庫
填寫構(gòu)建命令,點擊 Deploy site 按鈕
經(jīng)過一段時間的等待,不出意外應(yīng)用就部署成功了地址
注意在提交數(shù)據(jù)中一定要有 form-name 屬性,否則 Netlify 會接收不到數(shù)據(jù),返回 404 錯誤
輸入測試數(shù)據(jù),點擊提交就可以在 Netlify 的站點操作面板看到數(shù)據(jù)了

總結(jié)
以上所述是小編給大家介紹的在 Vue 應(yīng)用中使用 Netlify 表單功能的方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對創(chuàng)新互聯(lián)網(wǎng)站的支持!
網(wǎng)站題目:在Vue應(yīng)用中使用Netlify表單功能的方法詳解
網(wǎng)頁URL:http://www.chinadenli.net/article8/gisoop.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、品牌網(wǎng)站建設(shè)、網(wǎng)站維護(hù)、App設(shè)計、微信小程序、網(wǎng)站策劃
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)