vue3初识

news/2024/7/10 0:03:30 标签: vue
vue3_0">1.vue3中变量写法
import { reactive} from "vue";
export default {
  setup() {
    const state = reactive({
      value: "",
      ids: "",
      bannerImg: "",
      sightName: "",
      gallaryImgs: [],
      categoryList: []
    });
    //注意return出去
    return { state};
  }
 }
//在template中的变量前面加state
<p class="sightName">{{state.sightName}}</p>
vue3state_24">2.vue3变量省略state
//引入toRefs在return是...toRefs(state)就可以了
import { reactive,toRefs} from "vue";
export default {
  setup() {
    const state = reactive({
      value: "",
      ids: "",
      bannerImg: "",
      sightName: "",
      gallaryImgs: [],
      categoryList: []
    });
    
    return { ...toRefs(state)};
  }
};
<p class="sightName">{{sightName}}</p>
vue3this_47">3.vue3中不能用this
import { reactive,getCurrentInstance} from "vue";
export default {
  setup() {
    const state = reactive({
      value: "",
      ids: "",
      bannerImg: "",
      sightName: "",
      gallaryImgs: [],
      categoryList: []
    });
    //ctx就和vue2中this是一样的
    const { ctx } = getCurrentInstance();
    const go = () => {
      ctx.$router.go("-1");
    };
    return { state, go};
  }
};
vue3_70">4.vue3中事件
	<p class="go" @click="go">
        <van-icon name="arrow-left" />
      </p>
import { reactive,getCurrentInstance} from "vue";
export default {
  setup() {
  	const { ctx } = getCurrentInstance()
    const go = () => {
      ctx.$router.go("-1");
    };
    return { state, go};
  }
 }
vue3_91">5.vue3生命周期
import { reactive, onMounted} from "vue";
export default {
  setup() {
    onMounted(() => {
      ctx.$axios.get("/mock/detail.json").then(res => {
        console.log(res);
        state.bannerImg = res.data.data.bannerImg;
        state.gallaryImgs = res.data.data.gallaryImgs;
        state.sightName = res.data.data.sightName;
        state.categoryList = res.data.data.categoryList;
      });
    });
    return { state, onMounted};
  }
};
vue3axios_109">6.vue3安装axios

通过指令 npm install axios -S 安装axios和指令npm install vue-axios -S
安装vue-axios(注意:有时候我们在pack.json中看到vue-axios已经安装上去了,但是可能存在明明安装了,在组件中无法使用,因此需要重新安装vue-axios,这个在vue3.0中有80%的几率发生)

//在main.js全局配置axios
const app = createApp(App)
app.config.globalProperties.$axios = axios
vue3refrefvue2refvu3refvue2refDom_119">6.vue3ref(注意这个ref和vue2中的ref不一样,千万别弄混了,vu3中的ref是给定的值创建一个响应式的数据对象。vue2中的ref是获取Dom元素
//自增
import {reactive, ref} from "vue";
export default {
  name: "Home",
  setup() {
    const state = reactive({
      value: "",
    });
    const count = ref(11111)
    return { state,count };
  }

http://www.niftyadmin.cn/n/1367738.html

相关文章

require与import的区别和使用(CommonJS规范和es6规范)

CommonJS的由来 在早期没有模块化思想时代码没有很好的编写规范&#xff0c;导致很多代码、逻辑重复、缺乏条理性且不易管理维护&#xff0c;这也让很多开发者头疼不已。随着JavaScript 社区的发展&#xff0c;社区为JavaScript制定了相应的规范&#xff0c;而CommonJS规范的提…

elementUi中tree组件获取父节点的id

在tree组件中用current-change"getLeafKeys"事件 methods中 &#xff08;b是tree组件获取的node&#xff09; getLeafKeys(a,b){var parentList []function getParent (node) {// 判断当前节点是否有父节点&#xff0c;并且父节点上的data不能是数组if (node.paren…

搜素模糊查询可以大小写

//leafname 查询输入的值 nodes循环的数据 path内部使用 function findPathByLeafId(leafname, nodes, path) {if (path undefined) {path []}for (var i 0; i < nodes.length; i) {var names nodes[i].name if (names.toLowerCase().includes(leafname) true) {path.…

jsplumb连线的数据

var g_MyJsPlumb null;funInitJSPlumb function() {g_MyJsPlumb jsPlumb.getInstance({Endpoint:["Dot", { radius: 7 }], // 端点形状ConnectionOverlays: [["Arrow", { width: 12, length: 12, location: 1 }]], // 连线样式 装饰属性 箭头Connecto…

点击空白页面隐藏弹窗

<p class"parameter" v-clickoutside"handleClose" click"processParameters">点击</p><script> const clickoutside {// 初始化指令bind(el, binding, vnode) {function documentHandler(e) {// 这里判断点击的元素是否是本…

正则将回车或换行转换成逗号

//将将回车后换行转换成逗号 String.replace(/\r|\n/g,,) //字符串将逗号转换成换行 String.replace(/[\,]/g,\n)

element ui中el-tree横向和竖向滚动条问题

.tree{overflow-y: hidden;overflow-x: scroll;width:100%;height: 700px;overflow: auto;}.el-tree {min-width: 100%;display:inline-block !important;}

复制json对象的按钮

var input document.createElement("input") // 直接构建input input.value JSON.stringify(this.contentJson) // 设置内容 document.body.appendChild(input) // 添加临时实例 input.select() document.execCommand(Copy) document.body.removeChild(inpu…