最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue 如何打開(kāi)接口返回的HTML文件

 更新時(shí)間:2022年01月21日 14:41:50   作者:wuyuezhaolu  
本文主要介紹了vue 如何打開(kāi)接口返回的HTML文件,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

前言:接口測(cè)試平臺(tái),后端使用django,前端使用vue+element。項(xiàng)目接口平臺(tái)測(cè)試完成,需要把后臺(tái)產(chǎn)生的測(cè)試報(bào)告返回給前端展示。

一、后端接口

    1、配置下django的template的參數(shù),templates文件夾是放在project的目錄下面的,是項(xiàng)目中或者說(shuō)項(xiàng)目中所有的應(yīng)用公用的一些模板

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'userfiles')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

2、視圖函數(shù),讀取template目錄下report文件夾的報(bào)告,并返回給前端

 def getReport(self, request):
        # 獲取POST BODY信息
        reportId = request.data["id"]
        try:
            print(os.getcwd()) # 打印程序運(yùn)行目錄
            template = loader.get_template(f"./report/{reportId}.html")
            # template = loader.get_template(f"1.html")
            return Response(template.render())
            # return HttpResponse(template.render())
        except Exception as e:
            content = {'message': '獲取報(bào)告失敗'}
            return Response(content)

二、前端

1、如果后端返回的HTML文件不包含js文件,可以使用vue的v-html,vue的v-html只能渲染html元素和css文件,,不能執(zhí)行js文件

2、后端返回的數(shù)據(jù),HTML文件包含js文件。使用下面這種方法,接口獲取到的html數(shù)據(jù)在暫存的本地localStorage,再讀取數(shù)據(jù),然后在新窗口打開(kāi)報(bào)告。

接口返回的數(shù)據(jù)如下:

 template:

 <el-button type="warning" @click="viewReport" :disabled="reportDisabled

methods:

 
 // 查看測(cè)試報(bào)告
  viewReport () {
    var message = {id:1}
    //  axios 通過(guò)接口獲取后臺(tái)的報(bào)html告文件數(shù)據(jù)
    getReport(message).then(res => {
      this.$message({
        showClose: true,
        message: res.data.message,
        type: 'success'
      })
      // res.data 為接口返回的html完整文件代碼
       // 必須要存進(jìn)localstorage,否則會(huì)報(bào)錯(cuò),顯示不完全
      window.localStorage.removeItem('callbackHTML')
      window.localStorage.setItem('callbackHTML', res.data)
    // 讀取本地保存的html數(shù)據(jù),使用新窗口打開(kāi)
      var newWin = window.open('', '_blank')
      newWin.document.write(localStorage.getItem('callbackHTML'))
      // 關(guān)閉輸出流
      newWin.document.close()
    }).catch(err => {
      this.$message({
        showClose: true,
        message: err.response.msg,
        type: 'error'
      })
    })
  }

至此結(jié)束,點(diǎn)擊下按鈕即可在新窗口展示測(cè)試報(bào)告了

 到此這篇關(guān)于vue 如何打開(kāi)接口返回的HTML文件的文章就介紹到這了,更多相關(guān)vue 打開(kāi)接口返回的HTML文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

永宁县| 霍林郭勒市| 无极县| 福州市| 蒲城县| 金乡县| 东台市| 宿迁市| 彭水| 池州市| 夏邑县| 祁阳县| 沙坪坝区| 凤山县| 黔江区| 静安区| 响水县| 江口县| 确山县| 济源市| 湖南省| 广元市| 信宜市| 大英县| 本溪市| 吕梁市| 孟连| 竹北市| 蓬安县| 绥德县| 名山县| 正蓝旗| 奎屯市| 巴中市| 锡林郭勒盟| 葫芦岛市| 新营市| 梧州市| 仙居县| 会泽县| 舞钢市|