鍍金池/ 問答/PHP  HTML/ vue 渲染為php template 問題

vue 渲染為php template 問題

vue 宣傳頁預(yù)渲染成PHP template問題

用vue做的宣傳單頁,build 后的頁面只有一個(gè)div 標(biāo)簽,內(nèi)容都在js 文件用,最關(guān)鍵的是一些頁面需要php變量 輸出一些數(shù)據(jù),這些數(shù)據(jù)是無狀態(tài)的,異步請(qǐng)求浪費(fèi)一次請(qǐng)求,需求希望在用php 模板直接報(bào)一些數(shù)據(jù)渲染到頁面中,一次請(qǐng)求,加載出所有頁面的數(shù)據(jù)。

嘗試

用 prerender-spa-plugin 做預(yù)渲染,頁面中沒有php 直出的數(shù)據(jù)效果還可以,如果頁面中需要php 變量輸入數(shù)據(jù) 如:{$list} 得到一個(gè)列表,預(yù)渲染出來的html 就沒有數(shù)據(jù)了。
嘗試配置 externals 把php 輸出數(shù)據(jù)當(dāng)變量 應(yīng)用,但沒有效果。

相關(guān)代碼

  externals: {
        pagedata: 'pagedata'
    },
    // prerender-spa-plugin
    plugins:[
    new PrerenderSPAPlugin({
          // Index.html is in the root directory.
          staticDir: path.join(__dirname, '../dist'),
          routes: ['/'],
          // Optional minification.
          minify: {
              collapseBooleanAttributes: false,
              collapseWhitespace: false,
              decodeEntities: false,
              keepClosingSlash: false,
              sortAttributes: false
          },

          // renderer: new Renderer({
          //     renderAfterTime: 5000,
          // })
      })
     ]
     
     
    // 不用webpack 時(shí) 單例 vue  {$user} PHP模板變量
      data: {
                haha:{$user.haha_balance},
                balance:{$user.balance},
        }
     

需要拿到PHP 模板中的 變量數(shù)據(jù), 然后后預(yù)渲染忽略這些變量,但能在最后build 的html 中出現(xiàn)。

回答
編輯回答
妖妖

你想讓php渲染模板,那么你不能用vue-cli生成項(xiàng)目,你只能用<script>引入vue,這種場景下,var a = {$user};php就可以直接渲染好啊,然后你js調(diào)用你這個(gè)變量就可以了啊

2017年8月24日 01:47
編輯回答
何蘇葉

你要php來渲染數(shù)據(jù),那用vue的意義又是什么

2018年6月21日 09:47