Javascript 在没有ajax或api的情况下集成前端和后端的最佳方法

Javascript 在没有ajax或api的情况下集成前端和后端的最佳方法,javascript,php,integration,Javascript,Php,Integration,我想在前端框架中使用php变量,比如Vue js 集成前端和后端框架的最佳方式是什么 这是我的想法,但我认为有更好的方法 <script id = "data" > let $user = <?= json_encode($user) ?> </script > Some content... <script > new Vue({ data: { user: $user }, mounted

我想在前端框架中使用php变量,比如Vue js

集成前端和后端框架的最佳方式是什么

这是我的想法,但我认为有更好的方法

<script id = "data" >
    let $user = <?= json_encode($user) ?>
</script >
Some content... 

<script >
new Vue({
    data: {
        user: $user
    }, 

    mounted() {
        $("#data"). remove () 
    } 
}) 

让$user=
一些内容。。。
新Vue({
数据:{
用户:$user
}, 
安装的(){
$(“#数据”)。删除()
} 
}) 

虽然“简单性”很好,“功能性”也很关键

有时,您可以使用您的编码类型(例如,将其用于加载页面所需的PHP文件中的某些内容),并且您所拥有的可能适用于这种特定情况(不,我看不出有任何方法可以使其“更好”…),尽管大多数页面将需要更多的“流动”数据,您将很快用完只能编写“简单”代码的项目

学习使用ajax(一旦掌握了诀窍,它就非常简单)并从您自己的“库”中复制/粘贴(将代码片段保存在您记得的地方-您会发现许多您想要保存的东西…-我保存了一个“functions.php”文件,多年来它已经变得非常大,具有非常好的位和位。)

由于您已经在使用jQuery,这里有一种实现ajax的方法。。。(还有其他人,再一次,学习并找到你喜欢的方式…)

因此,正如您所看到的,只有几行代码可以添加Ajax,而且一旦您这样做了,您就可以做很多事情,所以请学习并使用它


快乐编码

有什么理由不使用Ajax或API吗?@magnus有!Simplicity在需要时使用Ajax获取所需数据,这比在需要时输出页面上的所有数据要好得多。浏览器也可以通过这种方式更好地缓存页面。使用jQuery执行Ajax请求(您似乎正在使用jQuery)只需再编写几行代码,并不复杂。
var url = "https://theURLtoMyAjax.phpPage";
var elements = "theStuff=thatIwantToSend&someMore=somethingElse"; // this is like writing everything in the address bar - again, there are other ways...)
$.post(url, elements, function (data) {
        // do all kinds of wonderful things in here!
        // the 'data' is what is returned from your call, so you can use it to update data on the page, etc. 
    });