【发布时间】:2020-04-01 02:22:11
【问题描述】:
我在我的 checkout.blade.php 文件中集成了 Stripe.js。它可以工作,但并非没有此错误:
[Vue warn]: Error compiling template:
Templates should only be responsible for mapping the state to the UI. Avoid placing tags with side-effects in your templates, such as <script>, as they will not be parsed
我已使错误消失,但条纹元素不会出现在页面上。我还在此文件中集成了其他 JS 用于我的表单验证,它工作正常且不返回错误。
这是返回错误的 Stripe 代码:
<script>
(function(){
// Create a Stripe client.
var stripe = Stripe('pk_test_9dn1vt3i0j0Q5GZdwAXn9iUs00iMziQDyD');
// Create an instance of Elements.
var elements = stripe.elements();
// Custom styling can be passed to options when creating an Element.
// (Note that this demo uses a wider set of styles than the guide below.)
var style = {
base: {
color: '#32325d',
fontFamily: '"Helvetica Neue", Helvetica, sans-serif',
fontSmoothing: 'antialiased',
fontSize: '16px',
'::placeholder': {
color: '#aab7c4'
}
},
invalid: {
color: 'red',
iconColor: 'red'
}
};
// Create an instance of the card Element.
var card = elements.create('card', { style: style, hidePostalCode: true });
// Add an instance of the card Element into the `card-element` <div>.
card.mount('#card-element');
// Handle real-time validation errors from the card Element.
card.addEventListener('change', function(event) {
var displayError = document.getElementById('card-errors');
if (event.error) {
displayError.textContent = event.error.message;
} else {
displayError.textContent = '';
}
});
// Handle form submission.
var form = document.getElementById('payment-form');
form.addEventListener('submit', function(event) {
event.preventDefault();
stripe.createToken(card).then(function(result) {
if (result.error) {
// Inform the user if there was an error.
var errorElement = document.getElementById('card-errors');
errorElement.textContent = result.error.message;
} else {
// Send the token to your server.
stripeTokenHandler(result.token);
}
});
});
// Submit the form with the token ID.
function stripeTokenHandler(token) {
// Insert the token ID into the form so it gets submitted to the server
var form = document.getElementById('payment-form');
var hiddenInput = document.createElement('input');
hiddenInput.setAttribute('type', 'hidden');
hiddenInput.setAttribute('name', 'stripeToken');
hiddenInput.setAttribute('value', token.id);
form.appendChild(hiddenInput);
// Submit the form
form.submit();
}
})();
</script>
请注意,现在 Stripe 代码嵌套在我的:
@section('content')
@endsection
由我的父模板 '''app.blade.php''' 扩展。
如果我将它粘贴到父模板之外并用这样的东西包装它:
@section('stripe')
@endsection
然后没有错误返回,但元素不会出现,因为我的 JS 不知道在哪里安装我的 Stripe 元素。当然,我的父模板中也有这个:@yield('stripe')
如何在不出现此模板错误的情况下显示我的 Stripe 元素?
编辑:我使用了 Laravel/ui 脚手架(Bootstrap 版本)。附件如下:
app.js:
require('./bootstrap');
window.Vue = require('vue');
const app = new Vue({
el: '#app',
});
ExampleComponent.vue:
<template>
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">Example Component</div>
<div class="card-body">
I'm an example component.
</div>
</div>
</div>
</div>
</div>
</template>
<script>
export default {
mounted() {
console.log('Component mounted.')
}
}
</script>
谢谢
【问题讨论】:
-
请添加您的 VueJS 代码,不仅仅是导致错误的块,而是实际的封装块。例如;这是在组件中吗?
-
@Daedalus-ReinstateMonica 不,我不是故意的,我只是复制粘贴错了。
-
你可能没有接触过 Vue,但你的代码显然是错误的。 Laravel 从 Vue 脚手架开始,所以请展示你的其余代码,因为没有它你的问题是无法解决的。
-
另外指定您是否使用了
laravel/ui框架。 -
laravel/ui框架为您的应用添加了一个视图,特别是this one;正如您将通过突出显示的行注意到的那样,主要内容 div 的 id 为app,如您的app.js文件所示,第 6 行指定了该 id。如果您只是删除该文件的第 3-7 行(app.js 之一),然后重新运行 laravel mix,您的代码应该可以正常工作。
标签: javascript laravel stripe-payments laravel-blade