vue+element-ui怎么实现打开页面就能自动弹出一个弹框,(里面包含一些文本信息)这种功能?
弹框样式如下图
我这里给你一个示例代码,你只需要把我这里p标签的内容替换成你要的内容就行了
<template>
<!-- 定义弹框组件 -->
<el-dialog title="提示" :visible.sync="dialogVisible">
<p>这里是弹框内容</p>
</el-dialog>
</template>
<script>
// 在data中定义弹框可见性数据,默认为true
export default {
data() {
return {
dialogVisible: false
}
},
// 在页面加载时将该数据设置为true,就会自动弹出弹框
mounted() {
this.dialogVisible = true
}
}
</script>
可以在组件挂载完成之后,在生命周期函数中调用 Element UI 的弹框组件来显示弹框,可以在 mounted
或 created
事件中调用。
以下是一个简单的示例:
<template>
<div>
<!-- 页面内容 -->
</div>
</template>
<script>
export default {
created() {
this.$nextTick(() => {
this.$alert('这是一个弹框', '提示', {
confirmButtonText: '确定'
});
});
}
};
</script>
在 created
生命周期事件中,我们使用 this.$nextTick
方法来等待组件挂载完成,然后调用 Element UI 提供的 $alert
弹框组件来显示弹框。在示例中我们只传入了弹框的文本信息,也可以通过参数来指定弹框的标题、按钮等信息。
<el-table ref="table"
:data="tableData"
style="width: 100%;margin-bottom: 20px;"
row-key="id"
border
:indent="50"
:header-cell-style="{background:'#F2F2F2'}"
:select-on-indeterminate="false"
@select="select"
@select-all="selectAll"
@selection-change="selectionChange"
default-expand-all
:tree-props="{children: 'childList'}">
<el-table-column type="selection"
:selectable='selectInit'
width="55">
</el-table-column>
<el-table-column prop="date"
label="日期"
sortable
width="180">
</el-table-column>
<el-table-column label="版本号"
width="">
<template slot-scope="scope">
<el-select v-model="scope.row.version"
@change="change(scope.$index,scope.row)">
<el-option v-for="item in roleData"
:key="item.id"
:label="item.name"
:value="item.id"></el-option>
</el-select>
</template>
</el-table-column>
<el-table-column prop="address"
label="地址">
</el-table-column>
</el-table>
js代码:
export default {
data () {
return {
checkPageAll: false,
tableData: [{
id: 1,
date: '2016-05-01',
address: '上海市普陀区金沙江路1弄'
}, {
id: 2,
date: '2016-05-02',
address: '上海市普陀区金沙江路 2 弄'
}, {
id: 3,
date: '2016-05-03',
address: '上海市普陀区金沙江路 3 弄',
childList: [{
id: 31,
date: '2016-05-31',
address: '上海市普陀区金沙江路 4 弄'
}, {
id: 32,
date: '2016-05-32',
address: '上海市普陀区金沙江路 5 弄'
}]
}, {
id: 4,
date: '2016-05-04',
address: '上海市普陀区金沙江路 6 弄'
}],
selectArr: [],
roleData: []
};
},
methods: {
change (index, row) {
console.log(index, row)
//设置多选框某些项不可多选
selectInit (row) {
// console.log(index)
if (row.id == 3) {
return false //不可勾选
} else {
return true //可勾选
}
},
select (selection, row) {
if (selection.some(el => { return row.id === el.id })) {
if (row.childList) {
row.childList.map(j => {
this.toggleSelection(j, true)
})
}
} else {
if (row.childList) {
row.childList.map(j => {
this.toggleSelection(j, false)
})
}
}
},
selectAll (selection) {
// tabledata第一层只要有在selection里面就是全选
const isSelect = selection.some(el => {
const tableDataIds = this.tableData.map(j => j.id)
return tableDataIds.includes(el.id)
})
// tableDate第一层只要有不在selection里面就是全不选
const isCancel = !this.tableData.every(el => {
const selectIds = selection.map(j => j.id)
return selectIds.includes(el.id)
})
if (isSelect) {
selection.map(el => {
if (el.childList) {
el.childList.map(j => {
this.toggleSelection(j, true)
})
}
})
}
if (isCancel) {
this.tableData.map(el => {
if (el.childList) {
el.childList.map(j => {
this.toggleSelection(j, false)
})
}
})
}
},
selectionChange (selection) {
this.selectArr = selection
console.log(this.selectArr, '0909')
},
toggleSelection (row, select) {
if (row) {
this.$nextTick(() => {
this.$refs.table && this.$refs.table.toggleRowSelection(row, select)
})
}
},
}
};
可以通过在main.js中添加mounted钩子,在页面加载后自动弹出一个包含文本信息的弹框。具体实现步骤如下:
<el-dialog title="欢迎使用" :visible="true">
<p>这是一个自动弹出的弹框,欢迎使用本系统。</p>
</el-dialog>
import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
Vue.config.productionTip = false
Vue.use(ElementUI)
new Vue({
router,
store,
render: h => h(App),
mounted() {
this.$alert('欢迎使用本系统!')
}
}).$mount('#app')
this.$alert('欢迎使用本系统!', '提示', {
confirmButtonText: '确定',
type: 'success',
center: true
})
其中,confirmButtonText表示确认按钮的文本,type表示提示框类型(success、warning、info、error中的一种),center表示文本居中显示。
let isFirstLoad = true
mounted() {
if (isFirstLoad) {
this.$alert('欢迎使用本系统!', '提示', {
confirmButtonText: '确定',
type: 'success',
center: true
})
isFirstLoad = false
}
}
这样,在页面首次加载时弹框会自动显示,并且在之后的页面刷新中不再自动弹出。
综上所述,通过在组件中添加el-dialog组件,在main.js中调用Vue.prototype.$alert方法,即可在vue+element-ui中实现页面打开时自动弹出一个包含文本信息的弹框。这里只是提供了一个简单的实现方案,具体的实现方式可以根据实际需要进行调整。